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
95%Fix Rate
88%Confidence
1Evidence
2022-12-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 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-systemWorkarounds
-
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; -
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.
中文步骤
Change next.config.js to use CommonJS syntax: /** @type {import('next').NextConfig} */ const nextConfig = { reactStrictMode: true }; module.exports = nextConfig;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:
-
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.
-
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 = ...