Next.js "Failed to load next.config.js" ESM / require error in CI
Next loads next.config at startup. Mixing ESM import/export syntax with a .js file in a CommonJS package, or a syntax error in the config, makes the load fail and the build cannot start.
What this error means
next build fails immediately with "Failed to load next.config.js" or "Error [ERR_REQUIRE_ESM]: require() of ES Module ... not supported", before any compilation.
Error: Failed to load next.config.js
[ERR_REQUIRE_ESM]: require() of ES Module /app/next.config.js
from /app/node_modules/next/... not supported.
Instead change the require to a dynamic import() or use next.config.mjs.Common causes
ESM syntax in a .js config without ESM package type
Using export default in next.config.js while the package is CommonJS makes Node refuse to require it.
A syntax or import error inside the config
Any throw while evaluating next.config, including a bad import, surfaces as a failure to load it.
How to fix it
Use the right config extension and syntax
- For ESM syntax, rename the file to
next.config.mjsand useexport default. - For CommonJS, keep
next.config.jsand usemodule.exports. - Re-run next build.
// next.config.mjs
const nextConfig = { /* ... */ }
export default nextConfigOr set the package type to module
If you want ESM in .js, set the package type so Node treats .js as ESM consistently.
// package.json
{ "type": "module" }How to prevent it
- Match config syntax to the file extension (.mjs for ESM).
- Keep next.config side-effect free so it always evaluates.
- Type-annotate the config to catch errors before the build.