Skip to content
Latchkey

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.

next build
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

  1. For ESM syntax, rename the file to next.config.mjs and use export default.
  2. For CommonJS, keep next.config.js and use module.exports.
  3. Re-run next build.
next.config.mjs
// next.config.mjs
const nextConfig = { /* ... */ }
export default nextConfig

Or 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
// 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.

Frequently asked questions

What causes ""Failed to load next.config.js""?
Using export default in next.config.js while the package is CommonJS makes Node refuse to require it.
How do I fix "Failed to load next.config.js"?
Use the right config extension and syntax

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card