i18n icon indicating copy to clipboard operation
i18n copied to clipboard

yarn build generate a prerender nitro error while yarn dev works fine

Open moulcode opened this issue 3 years ago • 21 comments

I wanna deploy my Nuxt 3 app but I get this error although I can run yarn preview but the i18n translation isn't working.

when running yarn build and yarn preview, error I get on the console in the browser: Screenshot 2023-05-22 at 11 01 37

When I run yarn build Screenshot 2023-05-22 at 10 54 18

i18n.config.ts: Screenshot 2023-05-22 at 10 55 44

nuxt.config.ts: Screenshot 2023-05-22 at 10 57 49

package.json Screenshot 2023-05-22 at 11 03 11

moulcode avatar May 22 '23 10:05 moulcode

Hi, thanks for opening an issue. It would be really helpful if you can provide a reproduction of this issue over Stackblitz please so that we can investigate/help better. You can use this as a starter - https://stackblitz.com/github/nuxt/starter/tree/v3-stackblitz

ineshbose avatar May 22 '23 12:05 ineshbose

Okay thank you I will reproduce the error

moulcode avatar May 22 '23 12:05 moulcode

I have the same issue when deploying in Netlify with pnpm

https://gregarious-kheer-2c80af.netlify.app/

You can check the source code here: https://github.com/JaimeTorrealba/renu-studios

And please let me know how to fix it

JaimeTorrealba avatar May 22 '23 21:05 JaimeTorrealba

Same Problem here, my package.json:

"@nuxtjs/i18n": "^8.0.0-beta.12", "nuxt": "^3.5.1",

dangercris avatar May 23 '23 02:05 dangercris

Hi @ineshbose here is a reproduction of the error: https://stackblitz.com/edit/github-7hsxua?file=app.vue

Thank you 🙏

moulcode avatar May 23 '23 09:05 moulcode

I get a similar error when I deploy on vercel or locally with npm run build .. local works fine

it seems it doesn't like export in config file which is similar to how others have defined it

one of the files comes with 500 in the browser so everything works except the translations when deployed

export default defineI18nConfig(() => ({
  legacy: false,
  locale: 'el',
  messages: {
    el: {
      index: {
          welcome: 'Καλωσηλθες'
      }
    },
    en: {
      index: {
          welcome: 'Welcome'
      }
    }
  }
}))

Browser Error:

Failed to load resource: the server responded with a status of 500 ()
i18n.options.2c8faeea.js:1 @nuxtjs/i18n Failed to fetch dynamically imported module: https://greek-evcars-git-i18n-nooul.vercel.app/__i18n__/prerender/cfcb45fa.js
u @ i18n.options.2c8faeea.js:1

 ERROR  (node:13072) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.             9:46:19 PM
(Use node --trace-warnings ... to show where the warning was created)


 ERROR  [nuxt] [request error] [unhandled] [500] Unexpected token 'export'                                                          9:46:19 PM
  at compileFunction (<anonymous>)
  at Object.compileFunction (node:vm:352:18)
  at wrapSafe (node:internal/modules/cjs/loader:1031:15)
  at Module._compile (node:internal/modules/cjs/loader:1065:27)
  at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
  at Module.load (node:internal/modules/cjs/loader:981:32)
  at Function.Module._load (node:internal/modules/cjs/loader:822:12)
  at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:190:29)
  at ModuleJob.run (node:internal/modules/esm/module_job:185:25)
  at async Promise.all (index 0)


 ERROR  (node:13072) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.             9:46:21 PM

  ├─ /__i18n__/prerender/301e041a.js (1569ms) (Error: [500] )
  

btw the above comment : https://github.com/nuxt-modules/i18n/issues/2097#issuecomment-1558910294 actually replicates the problem? I didn't see anything in the stackblitz terminal

zifnab87 avatar May 23 '23 18:05 zifnab87

Any news on this?

Rigo-m avatar May 24 '23 09:05 Rigo-m

so far no

moulcode avatar May 24 '23 15:05 moulcode

From v8.0.0-beta.12, we need to add "type": "module" to package.json of your nuxt project. https://v8.i18n.nuxtjs.org/getting-started/setup#packagejson

We are working on improving the system so that it can work without the "type": "module" specification, but at the moment that configuration is required.

kazupon avatar May 25 '23 01:05 kazupon

From v8.0.0-beta.12, we need to add "type": "module" to package.json of your nuxt project. https://v8.i18n.nuxtjs.org/getting-started/setup#packagejson

We are working on improving the system so that it can work without the "type": "module" specification, but at the moment that configuration is required.

@kazupon Thank you! while I had followed this suggestion from stackoverflow about a week ago and it makes i18n work.. translation still doesn't work with JavaScript off which is very important for SEO and search indexing. Any ideas why is that?

here is a vercel build: vercel build that can be used to illustrate it along with Disable Javascript Chrome Extension

image

Issue 1:

I added the following plugin (in addition to the i18n.config.js) and it now translation works without JavaScript enabled BUT lang switcher is not working so I cannot change languages (url changes with adding /en and then it doesn't remove it for greek language) .. there is need for documentation improvement I guess or it should somehow work out of the box without an extra plugin.. not sure about langswitcher ..

i18n.server.js

import { createI18n } from 'vue-i18n'
import en from '../locale/en.js'
import el from '../locale/el.js'

export default defineNuxtPlugin(({ vueApp }) => {
   const i18n = createI18n({
     legacy: false,
     globalInjection: true,
     locale: 'el',
     messages: {
         en, el
     }
   })

   vueApp.use(i18n)
 })

LangSwitcher.vue

<script setup>
const { locale } = useI18n()
const switchLocalePath = useSwitchLocalePath()
</script>
<template>
   <div class="language-switch">
       <div v-if ="locale === 'el'">
           <NuxtLink :to="switchLocalePath('en')"><img src="/uk.png" alt="English Flag" class="flag"/></NuxtLink>
       </div>
       <div v-else>
           <NuxtLink :to="switchLocalePath('el')"><img src="/el.webp" alt="Greek Flag" class="flag"/></NuxtLink>
       </div>
   </div>

</template>

Issue 2:

I found another issue when javascript is off:

in the data store (pinia) in one of the methods I am doing

const nuxt = useNuxtApp()
nuxt.$i18n.t('insightGenerator.monthly', { param }) 

and this part is not translated with javascript off

You can check again with this different vercel build (after introducing i18n.server.js plugin) along with Disable Javascript Chrome Extension to see how lang switcher doesn't behave properly with javascript off

zifnab87 avatar May 25 '23 11:05 zifnab87

Although I added the type module still the same issue when I run yarn build Screenshot 2023-05-25 at 13 19 55

moulcode avatar May 25 '23 12:05 moulcode

@wahibAbdou can't confirm - "type":"module" in package.json doesn't throw 500 for me anymore during build / browser run - but locally (not in vercel) build time takes 3-4 times more ~4 minutes. Also I have a lot of issues as I said above when javascript is off.. seems a lot of parts are not server rendered properly

zifnab87 avatar May 25 '23 12:05 zifnab87

Added type module to my package.json, still no translations in netlify environment

Rigo-m avatar May 25 '23 16:05 Rigo-m

Same here, type module doesn't fix the error image

JaimeTorrealba avatar May 25 '23 19:05 JaimeTorrealba

I just make it work in cloudflare the trick is add "type": "module", in package.json and not modify the nitro config, avoid this kind of config in nuxt.config.ts

nitro: {
    output: {
      dir: "output",
      serverDir: "output/server",
      publicDir: "output/public",
    },
  },

And in the deployment configuration for cloudflare put this: Captura de pantalla 2023-05-25 a la(s) 14 39 08

Works Like Charm!

Note: I'm using this versions of packages: "@nuxtjs/i18n": "^8.0.0-beta.12", "nuxt": "^3.5.1",

dangercris avatar May 25 '23 19:05 dangercris

Unfortunately, I had to roll back to the old versions where everything was working fine here are these versions that work for me @nuxtjs/[email protected] [email protected] hope this helps for now till this gets solved in future updates.

moulcode avatar May 25 '23 21:05 moulcode

First, I will comment before I respond.

Please comment if you are encountering the same problem as this issue. If the topic differs from the issue, open it as a new one. If so, please provide a minimal reproduction via GitHub repo or StackBliz! 🙏

kazupon avatar May 26 '23 01:05 kazupon

@wahibAbdou

Hi @ineshbose here is a reproduction of the error: https://stackblitz.com/edit/github-7hsxua?file=app.vue

Thank you for your reproduction. However, I could not reproduce your issue. So, I seem that your reproduction is not setup for issue enviroment. Would you setup your enviroment with minimal reproduction?

kazupon avatar May 26 '23 01:05 kazupon

Sorry I thought it was saved for some reason it wasn't lemme try again, thank you.

moulcode avatar May 26 '23 09:05 moulcode

@wahibAbdou can't confirm - "type":"module" in package.json doesn't throw 500 for me anymore during build / browser run - but locally (not in vercel) build time takes 3-4 times more ~4 minutes. Also I have a lot of issues as I said above when javascript is off.. seems a lot of parts are not server rendered properly

Actually this is what I'm experiencing. SSR rendering of translations is not working properly in netlify/vercel. Error has disappeared btw by adding type module, so I guess we should open another issue.

Rigo-m avatar May 26 '23 09:05 Rigo-m