Skip to content
>_VibeUnstuck

ReferenceError: window is not defined

Last verified:

The fix

window is not defined: Next.js read window, document, or localStorage on the server. Move that read into useEffect, or load the widget with ssr: false.

  1. 1

    Find the line in the stack

    The stack names your file or a dependency. If it is your code at module scope, move the read into useEffect or an event handler. Do not branch the rendered JSX on typeof window in a way that the server and the client return different trees. That trades this crash for a hydration mismatch.

  2. 2

    Load a browser-only widget without SSR

    For a chart, map, or editor that requires window at import time, load that component with next/dynamic and ssr: false. Do this for the widget, not for the whole page.

    import dynamic from "next/dynamic";
    
    const Chart = dynamic(() => import("@/components/chart"), {
      ssr: false,
      loading: () => <p>Loading chart…</p>,
    });
  3. 3

    Keep the data fetch on the server

    The page can stay a Server Component, fetch the data, and pass it into the client chart. Only the chart import is browser-only.

  4. 4

    Check a dependency that looks at window on import

    If the stack is inside node_modules, do not patch that file. Dynamic-import your wrapper, or configure the library’s SSR-safe entry if it has one. Pin the version once you know which release added the crash.

Why it happens

Next.js renders Client Components on the server to produce the first HTML. window, document, and localStorage are not there. The crash is usually at the top of a module (a library that reads window as soon as it is imported) or during render, not inside an event handler.

"use client" does not skip the server pass. A Vite SPA (plain Lovable or Bolt, without SSR) does not hit this unless something is actually rendering on a server. If you see it in Next.js, the import is running during SSR.

The next error you'll probably hit

Once this is fixed, the usual follow-up is useState only works in a Client Component. useState only works in a Client Component. This Next.js file is a Server Component until the first line is use client. Add that, or move the hook.

Prompt to paste back into your AI tool

Paste this above the error.

ReferenceError: window is not defined (or document / localStorage). The stack points at <file>.

Do not set ssr: false on the page. Do not add "use client" and then read window during render.

1. If our code reads window at module scope or during render, move it into useEffect or an event handler.
2. If a library crashes on import, load that component with next/dynamic and ssr: false, and give it a loading fallback.
3. Keep the rest of the page server-rendered.
4. Show only that split.

Common questions

+The file already has "use client". Why does window still crash?

Client Components are pre-rendered on the server. The directive marks the bundling boundary. It does not mean the module runs only in the browser.

+Can I check typeof window === "undefined" and return null?

You can return null on the server and content on the client, and React will warn about a hydration mismatch if the client’s first render does not also return null. Return null first, then fill the UI in useEffect, or use a dynamic import with ssr: false.

Related errors