# 错误：布局文件中重复导出 'metadata'。每个布局文件只允许一个 'metadata' 导出。

- **ID:** `nextjs/duplicate-export-metadata-layout`
- **领域:** nextjs
- **类别:** build_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

布局文件（layout.tsx）包含多个名为 'metadata' 的导出（例如，同时有静态 metadata 对象和 generateMetadata 函数），这是不允许的，因为 Next.js 每个布局只期望一个 metadata 定义。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Next.js 13.4.0 | active | — | — |
| Next.js 14.0.0 | active | — | — |
| Next.js 14.2.0 | active | — | — |

## 解决方案

1. ```
   删除静态 metadata 导出，只保留 generateMetadata 函数。示例：在 layout.tsx 中，删除 `export const metadata = { title: '...' }`，保留 `export async function generateMetadata() { return { title: '...' }; }`。
   ```
2. ```
   如果不需要动态逻辑，删除 generateMetadata 函数，只保留静态 metadata 对象。示例：在 layout.tsx 中，删除 generateMetadata 函数，保留 `export const metadata = { title: '...' }`。
   ```
3. ```
   如果同时需要静态和动态 metadata，将它们合并到一个 generateMetadata 函数中返回组合对象，或者使用静态 metadata 对象并在子页面中通过 generateMetadata 进行动态覆盖。
   ```

## 无效尝试

- **** — Next.js explicitly rejects duplicate exports during build; the build will fail with the same error. (100% 失败率)
- **** — Next.js only recognizes exports named exactly 'metadata' or 'generateMetadata'; renamed exports are ignored, so the metadata won't be applied. (95% 失败率)
- **** — The error is about the export name in the layout file itself; importing from another file still results in a single named export in the layout, so the duplicate is resolved, but if both are imported and re-exported, the same error occurs. (60% 失败率)
