Fix Error: Error: You are attempting to export "metadata" from a component marked with "use client", which is unsupported. in Next.js
This error occurs when a page or layout marked with 'use client' exports a metadata object or generateMetadata function. Metadata in Next.js App Router must be defined in server components. Fix it by removing 'use client' from the page or extracting metadata to a separate server component layout.
Reading the Stack Trace
Here's what each line means:
- at collectMetadata (/app/node_modules/next/dist/lib/metadata/resolve-metadata.js:98:11): The metadata resolver detected a metadata export in a client component, which is not supported by the App Router.
- at async resolveMetadata (/app/node_modules/next/dist/lib/metadata/resolve-metadata.js:204:20): Next.js resolves metadata from the component tree during server rendering and cannot process client component exports.
- at async renderToHTMLOrFlightImpl (/app/node_modules/next/dist/server/app-render/app-render.js:572:22): The App Router rendering pipeline fails when it encounters the incompatible metadata export.
Common Causes
1. 'use client' on a page with metadata
The page component has both 'use client' and a metadata export, but metadata must come from server components.
'use client';
import { useState } from 'react';
export const metadata = {
title: 'Dashboard',
description: 'User dashboard page',
};
export default function Dashboard() {
const [tab, setTab] = useState('overview');
return <div>{tab}</div>;
}
2. generateMetadata in client component
The page exports generateMetadata alongside 'use client', which is not allowed.
'use client';
export async function generateMetadata({ params }) {
const post = await fetch(`/api/posts/${params.slug}`).then(r => r.json());
return { title: post.title };
}
export default function PostPage({ params }) {
return <div>Post: {params.slug}</div>;
}
3. Entire page marked client for one interactive element
The developer added 'use client' to the entire page just to use useState in one section, inadvertently blocking metadata.
'use client';
export const metadata = { title: 'Products' };
export default function Products() {
const [filter, setFilter] = useState('');
return <input value={filter} onChange={(e) => setFilter(e.target.value)} />;
}
The Fix
Split the page into a server component (which exports metadata) and a client component (which uses useState). The server component page.tsx keeps the metadata export, and the interactive UI moves to a separate 'use client' component.
'use client';
import { useState } from 'react';
export const metadata = {
title: 'Dashboard',
description: 'User dashboard page',
};
export default function Dashboard() {
const [tab, setTab] = useState('overview');
return <div>{tab}</div>;
}
// app/dashboard/page.tsx (Server Component)
import DashboardClient from './DashboardClient';
export const metadata = {
title: 'Dashboard',
description: 'User dashboard page',
};
export default function Dashboard() {
return <DashboardClient />;
}
// app/dashboard/DashboardClient.tsx
'use client';
import { useState } from 'react';
export default function DashboardClient() {
const [tab, setTab] = useState('overview');
return <div>{tab}</div>;
}
Testing the Fix
import { render, screen } from '@testing-library/react';
import DashboardClient from '@/app/dashboard/DashboardClient';
describe('DashboardClient', () => {
it('renders with default tab', () => {
render(<DashboardClient />);
expect(screen.getByText('overview')).toBeInTheDocument();
});
});
describe('Dashboard metadata', () => {
it('exports correct metadata', async () => {
const { metadata } = await import('@/app/dashboard/page');
expect(metadata.title).toBe('Dashboard');
expect(metadata.description).toBe('User dashboard page');
});
});
Run your tests:
npm test
Pushing Through CI/CD
git checkout -b fix/metadata-client-component-split,git add src/app/dashboard/page.tsx src/app/dashboard/DashboardClient.tsx src/app/dashboard/__tests__/DashboardClient.test.tsx,git commit -m "fix: split dashboard into server and client components for metadata",git push origin fix/metadata-client-component-split
Your CI config should look something like this:
name: CI
on:
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- run: npm ci
- run: npm test -- --coverage
- run: npm run build
The Full Manual Process: 18 Steps
Here's every step you just went through to fix this one bug:
- Notice the error alert or see it in your monitoring tool
- Open the error dashboard and read the stack trace
- Identify the file and line number from the stack trace
- Open your IDE and navigate to the file
- Read the surrounding code to understand context
- Reproduce the error locally
- Identify the root cause
- Write the fix
- Run the test suite locally
- Fix any failing tests
- Write new tests covering the edge case
- Run the full test suite again
- Create a new git branch
- Commit and push your changes
- Open a pull request
- Wait for code review
- Merge and deploy to production
- Monitor production to confirm the error is resolved
Total time: 30-60 minutes. For one bug.
Or Let bugstack Fix It in Under 2 minutes
Every step above? bugstack does it automatically.
Step 1: Install the SDK
npm install bugstack-sdk
Step 2: Initialize
import { initBugStack } from 'bugstack-sdk'
initBugStack({ apiKey: process.env.BUGSTACK_API_KEY })
Step 3: There is no step 3.
bugstack handles everything from here:
- Captures the stack trace and request context
- Pulls the relevant source files from your GitHub repo
- Analyzes the error and understands the code context
- Generates a minimal, verified fix
- Runs your existing test suite
- Pushes through your CI/CD pipeline
- Deploys to production (or opens a PR for review)
Time from error to fix deployed: Under 2 minutes.
Human involvement: zero.
Try bugstack Free →No credit card. 5-minute setup. Cancel anytime.
Deploying the Fix (Manual Path)
- Split the page into server and client components.
- Run the Next.js build locally to confirm metadata is resolved.
- Open a pull request with the component split.
- Wait for CI checks to pass on the PR.
- Merge to main and verify metadata renders in page source.
Frequently Asked Questions
BugStack validates that metadata resolves correctly in the server component, tests the client component independently, runs your test suite, and confirms the build before marking it safe.
Every fix is delivered as a pull request with full CI validation. Your team reviews and approves before anything reaches production.
Yes. generateMetadata is an async function that runs on the server. You can fetch data, read params, and return dynamic titles and descriptions. It just cannot be in a 'use client' file.
Yes, significantly. Without proper metadata, search engines cannot read your page title, description, or Open Graph tags. Fixing this ensures your pages are properly indexed.