nextjs build_error ai_generated true

Error: Duplicate export 'metadata' in layout. Only one 'metadata' export is allowed per layout file.

ID: nextjs/duplicate-export-metadata-layout

Also available as: JSON · Markdown · 中文
90%Fix Rate
88%Confidence
1Evidence
2023-10-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Next.js 13.4.0 active
Next.js 14.0.0 active
Next.js 14.2.0 active

Root Cause

A layout file (layout.tsx) contains multiple exports named 'metadata' (e.g., both a static metadata object and a generateMetadata function), which is disallowed because Next.js expects exactly one metadata definition per layout.

generic

中文

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

Official Documentation

https://nextjs.org/docs/app/building-your-application/optimizing/metadata

Workarounds

  1. 95% success Remove the static metadata export and keep only the generateMetadata function. Example: In layout.tsx, delete `export const metadata = { title: '...' }` and keep `export async function generateMetadata() { return { title: '...' }; }`.
    Remove the static metadata export and keep only the generateMetadata function. Example: In layout.tsx, delete `export const metadata = { title: '...' }` and keep `export async function generateMetadata() { return { title: '...' }; }`.
  2. 90% success Remove the generateMetadata function and keep only the static metadata object if no dynamic logic is needed. Example: In layout.tsx, delete the generateMetadata function and keep `export const metadata = { title: '...' }`.
    Remove the generateMetadata function and keep only the static metadata object if no dynamic logic is needed. Example: In layout.tsx, delete the generateMetadata function and keep `export const metadata = { title: '...' }`.
  3. 85% success If both static and dynamic metadata are needed, merge them into a single generateMetadata function that returns the combined object, or use a static metadata object with dynamic overrides via generateMetadata in child pages.
    If both static and dynamic metadata are needed, merge them into a single generateMetadata function that returns the combined object, or use a static metadata object with dynamic overrides via generateMetadata in child pages.

中文步骤

  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 进行动态覆盖。

Dead Ends

Common approaches that don't work:

  1. 100% fail

    Next.js explicitly rejects duplicate exports during build; the build will fail with the same error.

  2. 95% fail

    Next.js only recognizes exports named exactly 'metadata' or 'generateMetadata'; renamed exports are ignored, so the metadata won't be applied.

  3. 60% fail

    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.