nextjs config_error ai_generated true

错误:next.config.js 必须使用 CommonJS 模块语法(module.exports = ...)或有效的 ESM 导出。

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

ID: nextjs/invalid-runtime-config-cjs

其他格式: JSON · Markdown 中文 · English
95%修复率
88%置信度
1证据数
2022-12-01首次发现

版本兼容性

版本状态引入弃用备注
13.0.0 active
13.5.0 active
14.0.0 active
14.2.0 active
15.0.0 active

根因分析

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

English

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

官方文档

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

解决方案

  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.

无效尝试

常见但无效的做法:

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

    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% 失败

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