# 错误：next.config.js 必须使用 CommonJS 模块语法（module.exports = ...）或有效的 ESM 导出。

- **ID:** `nextjs/invalid-runtime-config-cjs`
- **领域:** nextjs
- **类别:** config_error
- **验证级别:** ai_generated
- **修复率:** 95%

## 根因

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

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 13.0.0 | active | — | — |
| 13.5.0 | active | — | — |
| 14.0.0 | active | — | — |
| 14.2.0 | active | — | — |
| 15.0.0 | active | — | — |

## 解决方案

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.
   ```

## 无效尝试

- **Adding 'type': 'module' to package.json and using ESM syntax in next.config.js** — Next.js does not support ESM for next.config.js even if the package.json has type:module. It always expects CommonJS. (100% 失败率)
- **Using export default function nextConfig() { ... } which is valid ESM but not CommonJS** — Node.js require() cannot interpret export default; it expects module.exports = ... (95% 失败率)
