nextjs config_error ai_generated true

Error: next.config.js must use CommonJS module syntax (module.exports = ...) or be a valid ESM export.

ID: nextjs/invalid-runtime-config-cjs

Also available as: JSON · Markdown · 中文
95%Fix Rate
88%Confidence
1Evidence
2022-12-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
13.0.0 active — — —
13.5.0 active — — —
14.0.0 active — — —
14.2.0 active — — —
15.0.0 active — — —

Root Cause

Next.js loads next.config.js with Node.js require(), which expects CommonJS exports. Using ES module syntax like export default or export const without proper configuration causes a parse error.

generic

中文

Next.js 使用 Node.js require() 加载 next.config.js,期望 CommonJS 导出。使用 ES 模块语法如 export default 或 export const 而没有正确配置会导致解析错误。

Official Documentation

https://nextjs.org/docs/app/api-reference/next-config-js#module-system

Workarounds

  1. 95% success Change next.config.js to use CommonJS syntax: /** @type {import('next').NextConfig} */ const nextConfig = { reactStrictMode: true }; module.exports = nextConfig;
    Change next.config.js to use CommonJS syntax: /** @type {import('next').NextConfig} */ const nextConfig = { reactStrictMode: true }; module.exports = nextConfig;
  2. 90% success If you prefer ESM, rename the file to next.config.mjs and use export default; Next.js 14+ supports .mjs extension for ESM configs.
    If you prefer ESM, rename the file to next.config.mjs and use export default; Next.js 14+ supports .mjs extension for ESM configs.

中文步骤

  1. Change next.config.js to use CommonJS syntax: /** @type {import('next').NextConfig} */ const nextConfig = { reactStrictMode: true }; module.exports = nextConfig;
  2. If you prefer ESM, rename the file to next.config.mjs and use export default; Next.js 14+ supports .mjs extension for ESM configs.

Dead Ends

Common approaches that don't work:

  1. Adding 'type': 'module' to package.json and using ESM syntax in next.config.js 100% fail

    Next.js does not support ESM for next.config.js even if the package.json has type:module. It always expects CommonJS.

  2. Using export default function nextConfig() { ... } which is valid ESM but not CommonJS 95% fail

    Node.js require() cannot interpret export default; it expects module.exports = ...