Fix guides · Cursor, v0, Claude Code ·

Fix "ReferenceError: window is not defined" in Next.js

ReferenceError: window is not defined

This error means code that only works in a browser (window, document, navigator, localStorage) ran on the server, where those objects don't exist. Next.js renders pages on the server first, including components marked "use client". The usual fix is to move browser-only code into useEffect or an event handler, and to load browser-only libraries with next/dynamic and { ssr: false } from inside a Client Component.

Free · 3 a day · no signup

Common causes – and how to tell which one you have

1. Browser API used during render

How to tell: The stack trace points to a line inside a component's body or JSX that reads window, document, or localStorage directly, for example const width = window.innerWidth. This still fails in "use client" files because Client Components are also pre-rendered on the server.

Fix: Move the access into a useEffect, which runs only in the browser after the page loads, and store the result in state. Render a sensible default (such as null or a loading placeholder) until the effect has run.

2. Browser-only library imported normally

How to tell: The stack trace points into node_modules, often a map, chart, rich-text editor, or animation package such as Leaflet. These libraries touch window as soon as they are imported.

Fix: Import the component that uses the library with next/dynamic and { ssr: false } so it only loads in the browser. In the App Router this dynamic import must live in a file marked "use client", because ssr: false isn't allowed in Server Components.

3. Code at the top of a file

How to tell: The failing line is outside any function, at module level, e.g. const saved = localStorage.getItem('theme') at the top of a file or in a store or helper module. It runs as soon as the file is imported on the server.

Fix: Wrap that code in a function and call it only from useEffect or an event handler. For shared helpers, return a fallback when typeof window === 'undefined' instead of reading browser objects at import time.

4. useState initializer reading the browser

How to tell: You see something like useState(() => localStorage.getItem('x')) or useState(window.innerWidth). The initializer runs during the server render too.

Fix: Start with a neutral default in useState and set the real value from localStorage or window inside useEffect.

5. Browser code called from server-only code

How to tell: The error comes from a Server Component, route handler (route.ts), Server Action, middleware, or generateMetadata that imports a utility which uses window or document. These never run in a browser.

Fix: Remove the browser-dependent call from server code. Pass the needed values from the client (for example as request data) or move that logic into a Client Component.

How to fix it

  1. Open the full error in your terminal or Vercel build logs and find the first stack trace line that points to your own code (not node_modules), noting the file and line.
  2. If that line reads window, document, navigator or localStorage inside a component, move it into a useEffect and save the value in state with useState, using a safe default for the first render.
  3. Make sure the component using useEffect or useState has "use client" at the very top of its file.
  4. If the trace points into a library like a map, chart or editor, create a small "use client" wrapper file that imports it with const Map = dynamic(() => import('./Map'), { ssr: false }) and use that wrapper in your page.
  5. If the line is at the top of a file, outside any function, move it into a function that is only called from useEffect or an event handler.
  6. Run next build and then reload the page with a full refresh to confirm the error and any hydration warnings are gone.

Using Lovable, Bolt or v0?

In v0, paste the full error and ask: "Find the code that uses window, document or localStorage during server rendering, move it into useEffect, and load any browser-only library with next/dynamic and ssr: false from a 'use client' component." If the app is deployed on Vercel, open the failing deployment's build logs and copy the file name and line number from the stack trace into your prompt so the tool fixes the right spot. Ask it not to turn off server rendering for whole pages or layouts.

Using Cursor, Claude Code or Windsurf?

Give Cursor or Claude Code the complete stack trace, not just the first line, and ask it to name the exact file and line that runs on the server before changing anything. Have it run next build afterwards, since prerendering during the build surfaces this error reliably, and then load the affected page with a full browser refresh (not client-side navigation) while watching the terminal and browser console for both this error and hydration warnings.

The “fix” to avoid

Agents often wrap render code in if (typeof window !== 'undefined'), which stops the crash but makes the server and browser render different HTML, causing hydration mismatch errors, which they then hide with suppressHydrationWarning. Another common shortcut is loading an entire page or the root layout with ssr: false, which removes server rendering and hurts load speed and SEO. Just adding "use client" doesn't fix it either, because Client Components are still rendered on the server once.

Copy-paste fix prompt for your AI coding tool

Paste this into Cursor, Claude Code, Lovable or Bolt:

My Next.js app throws "ReferenceError: window is not defined". Using the full stack trace, identify the exact file and line running on the server before changing anything. Check in this order: 1) window, document, navigator or localStorage read during render or in a useState initializer; 2) a browser-only library (maps, charts, editors) imported normally; 3) module-level code touching browser APIs at import time; 4) server code (Server Components, route handlers, Server Actions, middleware) calling browser-dependent helpers. Fix by moving browser access into useEffect with state and a safe default, and loading browser-only libraries via next/dynamic with { ssr: false } from a "use client" wrapper. Don't use typeof window checks in render, suppressHydrationWarning, or ssr: false on whole pages or layouts. Verify by running next build and fully reloading the affected page with no errors or hydration warnings.
Your error looks a bit different? Paste your exact error and get a diagnosis tailored to your code — free.

Questions people ask

Why do I still get "window is not defined" after adding "use client"?

"use client" makes a component interactive, but Next.js still pre-renders it on the server once. Any window access during render will still crash there, so move it into useEffect or an event handler.

Can I use ssr: false in a Server Component?

No. In the App Router, next/dynamic with { ssr: false } must be used inside a file marked "use client". Create a small client wrapper component and import that from your page.

Is checking typeof window !== 'undefined' a good fix?

It's fine inside helper functions, event handlers or effects, but not for deciding what to render. Using it in render makes server and browser output differ, which causes hydration errors.

Official documentation

Related errors

Get fixes like this every week