How It Works Features Pricing Blog Error Guides
Log In Start Free Trial
Next.js · TypeScript/React

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

Error: You are attempting to export "metadata" from a component marked with "use client", which is unsupported. at getTracer (/app/node_modules/next/dist/server/lib/trace/tracer.js:32:15) at collectMetadata (/app/node_modules/next/dist/lib/metadata/resolve-metadata.js:98:11) at async resolveMetadata (/app/node_modules/next/dist/lib/metadata/resolve-metadata.js:204:20) at async renderToHTMLOrFlightImpl (/app/node_modules/next/dist/server/app-render/app-render.js:572:22) at async renderToHTMLOrFlight (/app/node_modules/next/dist/server/app-render/app-render.js:418:20) at async DevServer.renderToResponse (/app/node_modules/next/dist/server/base-server.js:1656:32) at async DevServer.run (/app/node_modules/next/dist/server/base-server.js:347:29) at async DevServer.handleRequest (/app/node_modules/next/dist/server/base-server.js:285:20)

Here's what each line means:

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.

Before (broken)
'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>;
}
After (fixed)
// 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:

  1. Notice the error alert or see it in your monitoring tool
  2. Open the error dashboard and read the stack trace
  3. Identify the file and line number from the stack trace
  4. Open your IDE and navigate to the file
  5. Read the surrounding code to understand context
  6. Reproduce the error locally
  7. Identify the root cause
  8. Write the fix
  9. Run the test suite locally
  10. Fix any failing tests
  11. Write new tests covering the edge case
  12. Run the full test suite again
  13. Create a new git branch
  14. Commit and push your changes
  15. Open a pull request
  16. Wait for code review
  17. Merge and deploy to production
  18. 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:

  1. Captures the stack trace and request context
  2. Pulls the relevant source files from your GitHub repo
  3. Analyzes the error and understands the code context
  4. Generates a minimal, verified fix
  5. Runs your existing test suite
  6. Pushes through your CI/CD pipeline
  7. 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)

  1. Split the page into server and client components.
  2. Run the Next.js build locally to confirm metadata is resolved.
  3. Open a pull request with the component split.
  4. Wait for CI checks to pass on the PR.
  5. 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.