Build with next-i18next error: Error: Cannot find module './next-i18next.config.js'

Hey guys!
I am having an issue with building a NextJS project with a next-i18next plugin.

My code is based on this example from the next-i18next repo: https://github.com/isaachinman/next-i18next/tree/master/examples/simple.

The only difference between my code and this repo’s code is that I have added

target: ‘serverless’

in next.config.js like this:

const { i18n } = require(‘./next-i18next.config.js’);

module.exports = {
target: ‘serverless’,
i18n,
}

After running

npm run build

I get this error:

next build

Loaded env from /home/kglogins/web-kglogins/.env
info - Creating an optimized production build
warn - Compiled with warnings

./node_modules/next/dist/next-server/server/load-components.js
Critical dependency: the request of a dependency is an expression

./node_modules/next/dist/next-server/server/load-components.js
Critical dependency: the request of a dependency is an expression

./node_modules/next/dist/next-server/server/require.js
Critical dependency: the request of a dependency is an expression

./node_modules/next/dist/next-server/server/require.js
Critical dependency: the request of a dependency is an expression

./node_modules/next/dist/next-server/server/require.js
Critical dependency: the request of a dependency is an expression

./node_modules/next-i18next/dist/commonjs/serverSideTranslations.js
Critical dependency: the request of a dependency is an expression

./node_modules/i18next-fs-backend/cjs/readFile.js
Critical dependency: the request of a dependency is an expression

./node_modules/i18next-fs-backend/cjs/readFile.js
Critical dependency: the request of a dependency is an expression

info - Collecting page data
[ ==] info - Generating static pages (0/5)Unhandled error during request: Error: Cannot find module ‘/home/kglogins/web-kglogins/next-i18next.config.js’
at webpackEmptyContext (/home/kglogins/web-kglogins/.next/serverless/pages/index.js:22452:10)
at /home/kglogins/web-kglogins/.next/serverless/pages/index.js:896:90 {
code: ‘MODULE_NOT_FOUND’
}

Error occurred prerendering page “/lv”. Read more: Prerender Error | Next.js
Error: Cannot find module ‘/home/kglogins/web-kglogins/next-i18next.config.js’
at webpackEmptyContext (/home/kglogins/web-kglogins/.next/serverless/pages/index.js:22452:10)
at /home/kglogins/web-kglogins/.next/serverless/pages/index.js:896:90
Unhandled error during request: Error: Cannot find module ‘/home/kglogins/web-kglogins/next-i18next.config.js’
at webpackEmptyContext (/home/kglogins/web-kglogins/.next/serverless/pages/index.js:22452:10)
at /home/kglogins/web-kglogins/.next/serverless/pages/index.js:896:90 {
code: ‘MODULE_NOT_FOUND’
}

What is more interesting is that my code compiles on the Vercel platform with 0 problems (even using next-on-netlify, I don’t know why… :D), but on Netlify platform I get this error.

1 Like

I have the same problem.

I used to have a site with these build settings:

Build command: next build && next export
Publish directory: out

After following the example in next-i18next/examples/simple at master · isaachinman/next-i18next · GitHub, my site deploy would fail, returning an error which stated that next export was incompatible with next-i18next.

Then I replaced the settings with:

Build command: next build
Publish directory: (empty)

But at step “4. onBuild command from @netlify/plugin-nextjs” I got “Your next.config.js must set the “target” property to one of: serverless, experimental-serverless-trace. Update the target property to allow this plugin to run.”. The build completed but the site only shows a 404 page.

Then I set the target: "serverless" option in next.config.js and now for every page in the deploy I get the message “Error occurred prerendering page “homepage”. Error: Cannot find module ‘/opt/build/repo/next-i18next.config.js’”.

The file is there in my GitHub repository, and the build works on Vercel as well.

Can someone help with the correct config values and build settings?

Ok, after trying almost everything, I found this setting that fixed my build. Instead of target: serverless use:

target: "experimental-serverless-trace",

Please anyone can explain why this setting is necessary to use next-i18next?

You can see my site online at:

And my site repository, if it helps:

My deploy settings are:

Build command: next build
Publish directory: Not set

2 Likes

I can confirm that @jguix solution worked for me as well (Next.js v10 + next-i18next v8) but I had to add typescript to my project’s dependencies, otherwise deploys would fail with

A Netlify Function failed to require one of its dependencies.
Cannot find module 'typescript'
2 Likes

Thank you for sharing, @jguix and @orangefiber :netliconfetti: We always appreciate when folks share their solutions and processes, as it helps future Forums members!

It worked for me too! :pray:t3:

1 Like