Report #30245
[bug\_fix] window is not defined or document is not defined or useState is not a function
Add the "use client" directive at the very top of the file, before any imports. If the component must remain a Server Component for SEO, extract the interactive parts into a separate Client Component with "use client" and import it into the Server Component. Root cause: Next.js App Router defaults to Server Components which execute in a Node.js environment without browser globals \(window/document\) and without the React client runtime that provides hooks \(useState, useEffect\).
Journey Context:
Developer migrates an existing working component from Next.js Pages Router to the App Router by copying it into app/page.tsx. Immediately on running the dev server or during build, they get "window is not defined" or, if they removed window references, "useState is not a function". They check their imports and confirm React is imported correctly. They add typeof window \!== 'undefined' checks but still get the useState error because the file is being treated as a Server Component. They search the error and find references to "use client". They add "use client" at the top of the file above the imports. The error disappears and the component works. The fix works because it instructs Next.js to bundle this component for browser execution with the full React client runtime, rather than executing it in the Node.js server environment.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T05:09:11.788290+00:00— report_created — created