next-i18next: Not working with Next.js >= 10.2.x (probably because of webpack 5, dynamic imports are handled differently)
Describe the bug
Seems with the update to Next.js 10.2, Next.js is by default using webpack 5: https://nextjs.org/blog/next-10-2#webpack-5
Occurs in next-i18next version
next-i18next: v8.5.1 Next.js: >= v10.2.x
Steps to reproduce
Just update the next version in the simple example: https://github.com/isaachinman/next-i18next/blob/master/examples/simple/package.json#L12
Error
I have no idea on how this works behind the scene, it may be Next.js itself needs to do something.
Currently an error like this is occurred:
Error: Cannot find module '/Users/user/Projects/next-i18next/examples/simple/next-i18next.config.js'
MODULE_NOT_FOUND
(happens on dev)
Additional context
I’m not very familiar with Next.js and/or webpack 5, so if someone wants to invest a bit of time to investigate, this would be really good.
Currently next-i18next only works with Next.js <= v10.1.x if not passing the userConfig in explicitely
There is also an issue in the Next.js repo: https://github.com/vercel/next.js/issues/24729
➡️ Seems like this is breaking because webpack 5 is handling dynamic imports differently: https://github.com/vercel/next.js/issues/25077
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 9
- Comments: 35 (21 by maintainers)
I have good news: Based on @myWsq’s discussion and example I’ve created a simple next-i18next example that works with SSG: https://github.com/adrai/next-i18next-static-example You see here, it is not using the in-built next.js i18n feature. with a new language-detector module: https://github.com/adrai/next-language-detector/tree/main/example
fyi: still no luck with next.js v11.1.0 😢
For those not following #1384, this has been linked to a bug in NextJs: https://github.com/vercel/next.js/issues/24700
No worries! As long as users are implementing
getStaticProps
, they can simply runnext build && next start
, and content will be served fully statically. No need to usenext export
.ModuleNotFoundError: Module not found: Error: Can't resolve 'fs' in '/vercel/path0/node_modules/next-i18next/dist/commonjs'
I get this webpack error upon production build on Vercel unfortunately. I can’t downgrade to webpack4 because my project is already complex & uses a lot of webpack5 features. The funny thing is everything works well in development environment
I found a temporary fix until https://github.com/vercel/next.js/issues/24700 gets addressed in Next.js Iteration 22 or 23, 24, 25, etc…
serverSideTranslations.ts
line 42, check for!userConfig
before importing fromDEFAULT_CONFIG_PATH
serverSideTranslations()
call,import nextI18NextConfig from '../next-i18next.config.js'
and pass it as last argument toserverSideTranslations
=>await serverSideTranslations(locale, ['common', 'footer'], nextI18NextConfig)
I just tested this on Next.js v11.0.1 and it seems to work.
serverSideTranslations.ts
i.e.
second-page.js
this may be related: https://github.com/vercel/next.js/issues/24700