next-i18next: Error: appWithTranslation was called without a next-i18next config
Describe the bug
Error: appWithTranslation was called without a next-i18next config
Occurs in next-i18next version
I tried the new version - 8.0.1
Steps to reproduce
-
next-i18next.config.js
module.exports = { i18n: { defaultLocale: 'en_US', locales: ['en_US', 'de_DE', 'fr_CH'], }, }
-
next.config.js const { i18n } = require(‘./next-18next.config’)
module.exports = { i18n, }`
- _app.js import { appWithTranslation } from ‘next-i18next’; import ‘…/styles/style.scss’; import ‘swiper/swiper-bundle.css’; import ‘swiper/components/navigation/navigation.scss’; import ‘react-calendar/dist/Calendar.css’;
const MyApp = ({ Component, pageProps }) => <Component {…pageProps} />
export default appWithTranslation(MyApp)
Screenshots
OS (please complete the following information)
- Device: Ubuntu 20.04 LTS
- Browser: Chrome 88.0.4324.182
Additional context
Add any other context about the problem here.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 14
- Comments: 30 (8 by maintainers)
Adding config to serverSideTranslations fixed it for me.
...(await serverSideTranslations(locale, ['common'], nextI18NextConfig)),
@BenAllenUK had the right approach. I ended up tracing through the same issue because I didn’t see the notes until after I was done 😃
Overview
I have a hand-crufted fix in hand, deployed in a real app, details in next section. I do not know how to make this as a patch that we can ship in the library that will work in both non-bundled and bundled cases with the module at the root of a monorepo or in a child package directory. I’m willing to do a Google Meet or other brainstorming / pairing session with anyone that has an idea to try out.
and I believe we can fix this by changing to(turns out theawait import
to prevent Webpack from pointing the config file import to the empty dummy using the technique for hownext.config.js
is avoiding this problemnext.config.js
approach only works becausenext
is external / not bundled).I am NOT using
target: serverless
(using this with Next.js 13.1.1) but that does not matter. I have a replacement fortarget: serverless
that I’ll be sharing shortly that is even better but does the same bundling ofnode_modules
to prevent enormous deployments and slow starts on Lambda. The solution below will apply gotarget: serverless
though as the problem is just Webpack.The hand-crufted Fix
node_modules/next-i18next/dist/commonjs/serverSideTranslations.js
return Promise.resolve("".concat(_path["default"].resolve(DEFAULT_CONFIG_PATH))).then(function (s) {
next.config.js
from below - This will cause the contents of the file to be bundled (so editing thenext-i18next.config.js
file at runtime will have no effect)next.config.js
Blocknode_modules/next-i18next/dist/commonjs/serverSideTranslations.js
BlockProblem Summary
node_modules/next-i18next/dist/commonjs/serverSideTranslations.js
requires the config file via a expression (path.resolve
primarily), resolved as a promise, which then evaluates another expression (path.resolve
again)require
to the empty module and fails at runtimefull dynamic
in webpack and were changed to point to the empty context: https://webpack.js.org/migrate/3/#full-dynamic-requires-now-fail-by-default.next/server/chunks
, making it very frustrating / misleading to try to fix this../**/next-i18next.config
in application code work fine because they are not an expression and webpack can find them at build timerequire
it will not be able to follow any directives inconfig.externals
such as{ 'next-i18next.config': './next-i18next.config.js' }
- The problem is not that the file is not externalled but rather that Webpack has no idea what this require is so it just points it to emptyThe Fix
The
path.resolve
call fromDEFAULT_CONFIG_PATH
has to be removed as a temporary workaround.Now… why does this work for the
next.js
import ofnext.config.js
?It appears that usingCorrection: this works forawait import(...)
is either a workaround or a legit way to say that we want this imported at runtime, in any case it appears that it stops Webpack from messing this up while still allowing discovery of the path at runtime.next
becausenext
is not bundled so the import expression is evaluated only at runtime.Applying the Fix Locally
patch-package
postinstall
script that runspatch-package
"postinstall": "patch-package --patch-dir ./patches/"
patches/next-i18next+13.0.3.patch
npm i
oryarn install
again to runpatch-package
npx patch-package next-i18next
to regenerate the patch - if that fails, apply the patch by hand-editing the file (make sure you get thecommonjs
version), then runnpx patch-package next-i18next
to regenerate the patchnext-i18next
is installed in thenode_modules
directory that is in the same directory asnext-i18next.config.js
- If these are different (e.g. next-i18next is a child of another module) then you’ll have to edit the number of../
in the pathrm -rf .next/cache
orrm -rf .next
next-i18next.config.js
if deployingpatches/next-i18next+13.0.3.patch
Patch File ContentsThe Line in
next-i18next
that Breaks Webpackhttps://github.com/i18next/next-i18next/blob/6a692ae688d4cb5c95bc8ff0a378f12a6a2fe61c/src/serverSideTranslations.ts#L37
How Next.js Imports the
next.config.js
at RuntimeThis works for
next
whennext
is not bundled. But this fails just the same if used innext-i18next
as it still uses an expression for the import.https://github.com/vercel/next.js/blob/70c087e4cf6188d5290b1fe32975b22e17b03b69/packages/next/src/server/config.ts#L922
👉 Why No One Has Confirmed a Fix for This 👈
Locally patching
node_modules/next-i18next/dist/commonjs/serverSideTranslations.js
and re-runningnext build
will show no change, driving you mad.The problem is that the
node_modules
are only compiled once and cached in.next/cache
.If you make a change to a file in
node_modules
you have torm -rf .next/cache
before you build again, then you will see that the resulting chunk file has your changes.next-i18next
Require ofnext-i18next.config.js
is an ExpressionTIL that it is crucial that the config file looks exactly like the one described in the README. I was getting this exact error scratching my head as to why it’s missing, then I realized my config file looks like this:
instead of this:
I hadn’t realized that was a load-bearing
i18n
keyI got the same error and for me the problem was that I use the
serializeConfig: false,
option. Once I added the configuration manually to appWithTranslation like thisappWithTranslation(MyApp, nextI18NextConfig)
it worked. I realize it is not the same config as the reporter’s one, but it may help someone.Interesting, thanks for a very detailed account.
I think what you’re suggesting would involve changing this import to a regular
require
. Do you want to give that a test, and see if it solves the issue?Nothing here worked for me. Seriously!
I had the same error and for me was the case was that I was not sending the correct props down to the pages.
I was sending in an object containing the key
_nextI18Next
:instead of the correct object:
I was not spreading the
...serverSideTranslations
and got confused that was the expected props.Hopefully, this will help someone.
Hi @hectorstudio – unfortunately I’m unable to determine what is going wrong based on the snippets you’ve copy/pasted. Can you please provide a full repo?
Also just a heads up, regional locales use dashes, not underscores. A correct locale would be
en-US
.