You're importing a module that depends on `useState` into a React Server Component module. This API is only available in Client Components. To fix, mark the file (or its parent) with the `"use client"` directive.

Next.js · updated 2026-11-04

Reported by the Next.js compiler (next dev and next build) when a file treated as a Server Component, which is every file in app/ by default, imports a React API that only runs in the browser: useState, useEffect, useReducer, useRef, createContext, class components. The import trace under the message shows which file pulled it in.

 ⨯ ./components/like-button.tsx
Error:   x You're importing a module that depends on `useState` into a React Server Component module. This API is only available in Client Components. To fix, mark the file (or its parent) with the `"use client"` directive.
Import trace for requested module:
  ./components/like-button.tsx
  ./app/posts/[id]/page.tsx

Quick fix

Put "use client" at the very top of the file that uses the hook (the first line, before imports), not on the page. Keep the page a Server Component and pass data to the client component as props. For a third-party component that uses hooks but ships without "use client", re-export it from a small file of your own that starts with "use client".

  1. Read the import trace: the first file listed is the one that uses the client-only API.
  2. Add "use client" as the first line of that file.
  3. Leave pages and layouts as Server Components; fetch data there and pass it down as props (props must be serializable).
  4. Third-party component without "use client": create a wrapper file with "use client" that re-exports it, and import the wrapper.
  5. If you moved the directive and still see it, make sure it's the first statement: a directive below an import is ignored.
typescript
// components/like-button.tsx
'use client'; // first line, before imports

import { useState } from 'react';

export function LikeButton({ initial }: { initial: number }) {
  const [likes, setLikes] = useState(initial);
  return <button onClick={() => setLikes(likes + 1)}>{likes} likes</button>;
}

// app/posts/[id]/page.tsx stays a Server Component:
// <LikeButton initial={post.likes} />

Why it happens

The directive is missing on the component

In the App Router every component is a Server Component unless its file (or a file that imports it from the client side) starts with "use client".

The directive is not the first statement

A "use client" line after imports or other code does not count. The compiler reports that separately: The "use client" directive must be placed before other expressions.

A library that uses hooks without the directive

Older UI and animation libraries call hooks or createContext but do not mark their files, so importing them directly into a Server Component fails.

Moving code from the Pages Router

Components that worked in pages/ (all client-rendered) fail when moved under app/, where the default is the server.

Other fixes

  • Mark only the interactive leaf components as client components; marking a whole page "use client" sends all of its code to the browser and loses server-only data fetching.
  • Context providers need their own "use client" file; render the provider in a layout and keep the layout itself a Server Component.

Same error, other wording

  • You're importing a component that needs useState. It only works in a Client Component but none of its parents are marked with "use client", so they're Server Components by default.
  • You're importing a module that depends on `useEffect` into a React Server Component module.
  • TypeError: createContext only works in Client Components. Add the "use client" directive at the top of the file to use it.

Let your coding agent catch the next one

Runheed records this error in production with the stack trace, the breadcrumbs before it and the agent session that shipped the code, and serves it to Claude Code, Cursor or any MCP client. With it connected, you can ask:

> Find the "only available in Client Components" build errors, follow the import trace to the file that uses the hook, and add "use client" to that component (or a wrapper for a third-party one) while keeping the page a Server Component.

Plans from $29/moSee what your agent readsSee the setupCancel any time from the billing portal

Related errors

More in React and Next.js rendering

References: Next.js docs: "use client" directive, Next.js docs: Server and Client Components, Next.js source: the compiler message (react_server_components.rs)