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 importto prevent Webpack from pointing the config file import to the empty dummy using the technique for hownext.config.jsis avoiding this problemnext.config.jsapproach only works becausenextis 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: serverlessthat I’ll be sharing shortly that is even better but does the same bundling ofnode_modulesto prevent enormous deployments and slow starts on Lambda. The solution below will apply gotarget: serverlessthough as the problem is just Webpack.The hand-crufted Fix
node_modules/next-i18next/dist/commonjs/serverSideTranslations.jsreturn Promise.resolve("".concat(_path["default"].resolve(DEFAULT_CONFIG_PATH))).then(function (s) {next.config.jsfrom below - This will cause the contents of the file to be bundled (so editing thenext-i18next.config.jsfile at runtime will have no effect)next.config.jsBlocknode_modules/next-i18next/dist/commonjs/serverSideTranslations.jsBlockProblem Summary
node_modules/next-i18next/dist/commonjs/serverSideTranslations.jsrequires the config file via a expression (path.resolveprimarily), resolved as a promise, which then evaluates another expression (path.resolveagain)requireto the empty module and fails at runtimefull dynamicin 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.configin application code work fine because they are not an expression and webpack can find them at build timerequireit will not be able to follow any directives inconfig.externalssuch 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.resolvecall fromDEFAULT_CONFIG_PATHhas to be removed as a temporary workaround.Now… why does this work for the
next.jsimport 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.nextbecausenextis not bundled so the import expression is evaluated only at runtime.Applying the Fix Locally
patch-packagepostinstallscript that runspatch-package"postinstall": "patch-package --patch-dir ./patches/"patches/next-i18next+13.0.3.patchnpm ioryarn installagain to runpatch-packagenpx patch-package next-i18nextto regenerate the patch - if that fails, apply the patch by hand-editing the file (make sure you get thecommonjsversion), then runnpx patch-package next-i18nextto regenerate the patchnext-i18nextis installed in thenode_modulesdirectory 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/cacheorrm -rf .nextnext-i18next.config.jsif deployingpatches/next-i18next+13.0.3.patchPatch File ContentsThe Line in
next-i18nextthat Breaks Webpackhttps://github.com/i18next/next-i18next/blob/6a692ae688d4cb5c95bc8ff0a378f12a6a2fe61c/src/serverSideTranslations.ts#L37
How Next.js Imports the
next.config.jsat RuntimeThis works for
nextwhennextis not bundled. But this fails just the same if used innext-i18nextas 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.jsand re-runningnext buildwill show no change, driving you mad.The problem is that the
node_modulesare only compiled once and cached in.next/cache.If you make a change to a file in
node_modulesyou have torm -rf .next/cachebefore you build again, then you will see that the resulting chunk file has your changes.next-i18nextRequire ofnext-i18next.config.jsis 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
i18nkeyI 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
...serverSideTranslationsand 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.