The Agent Ledger

← All fixes

Fix: "You're importing a module that depends on `useState`" when your AI-built Next.js app won't build

By The Agent Ledger. Drafted with AI assistance and reviewed by the publisher. Tested 2026-10-18.

Status: Reproduced and fixed on a synthetic app (build and server-rendered output only). We reproduced the error and verified the fix on 2026-10-18 with a tiny invented Next.js app on one Linux machine (Node 20.19.2, next 16.4.2, react 19.3.1). `next build` fails before the fix and passes after, and the built page's served HTML contains the button. We did NOT click the button in a real browser, test other Next.js versions, the Pages Router, Vercel/Netlify, or code generated by Cursor, Lovable, Bolt or v0. No real systems or private data were used.

Short answer: This error means a file that uses a React hook such as useState is being treated as a Server Component, which cannot hold state. The smallest fix is to move the interactive part into its own small file that starts with "use client", and import it from your page. Verified on a synthetic app only.

Prerequisites

The exact error

From our reproduction (npx next build, Next.js 16.4.2 with Turbopack):

Error: Turbopack build failed with 1 error:
./app/page.js:1:10
Error: 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.
    Learn more: https://nextjs.org/docs/app/api-reference/directives/use-client
> 1 | import { useState } from "react";
    |          ^^^^^^^^

Exit code: 1. Wording differs by version. Public reports show older text, such as "You're importing a component that needs useState. This React Hook only works in a Client Component" (for example a vercel/next.js discussion on the use client boundary and an AI-editor report in redwoodjs/sdk#803, where the author notes Cursor made the same mistake). We did not run those older versions and did not measure how common the error is; the same hook name may be useEffect, useRef etc.

Versions we used

Item Version
Node.js 20.19.2
next 16.4.2 (Turbopack build)
react / react-dom 19.3.1
OS Linux, single machine

Cause

In the Next.js App Router every file is a Server Component by default. Server Components render once on the server, so they cannot use state, effects or click handlers. AI tools often write a page that imports useState and uses onClick in the same file but forget the "use client" line.

Smallest working fix

Put the interactive piece in its own file with "use client" at the very top, and keep the page itself free of hooks.

Before (fails): app/page.js

import { useState } from "react";

export default function Home() {
  const [count, setCount] = useState(0);
  return (
    <main>
      <h1>Synthetic signup counter</h1>
      <button onClick={() => setCount(count + 1)}>Clicks: {count}</button>
    </main>
  );
}

After (builds)

app/Counter.js (new file):

"use client";
import { useState } from "react";

export default function Counter() {
  const [count, setCount] = useState(0);
  return <button onClick={() => setCount(count + 1)}>Clicks: {count}</button>;
}

app/page.js:

import Counter from "./Counter";

export default function Home() {
  return (
    <main>
      <h1>Synthetic signup counter</h1>
      <Counter />
    </main>
  );
}

Quicker alternative (not run by us as a separate test): add "use client"; as the first line of the original page.js. This is documented Next.js behavior, but it makes the whole page client code, so the split above is usually better.

Copyable prompt for your AI coding tool

My Next.js App Router build fails with "You're importing a module that depends on `useState` into a React Server Component module." Find every file without a "use client" directive that uses React hooks (useState, useEffect, useRef, useReducer) or event handlers like onClick. For each, move only the interactive part into a new small component file that starts with "use client", and import it into the original file. Do not add "use client" to pages that don't need it, do not change anything else, and show me the diff.

Review the diff yourself. We did not test this prompt in any AI tool.

How we verified

Run on 2026-10-18 in fix-02/before and fix-02/after.

  1. Before: npx next build failed with the error above, exit code 1 (before-build.log).
  2. After: npx next build exited 0 and listed / as a static route (after-build.log).
  3. After, served page: npx next start, then curl of / returned HTML containing <button>Clicks: <!-- -->0</button>, and the client JS chunk containing the string "Clicks" existed in .next/static/chunks (after-runtime.html).

Files: see the downloads below.

Not verified

Troubleshooting

Security notes

Downloads

Synthetic example files from our test. No real keys or data.

Get The Agent Ledger every Tuesday

Free. One practical AI automation per week, explained in plain language, with a clear monthly cost estimate and what you still do yourself.

Free to subscribe. One email a week, on Tuesdays. Unsubscribe any time. We use your email only to send the newsletter. Privacy