← All errors · Next.jsReact & Next.js rendering errors

ReferenceError: window is not defined

Exact error

ReferenceError: window is not defined

What it means

Code that touches a browser global (window, document, localStorage) ran on the server during server rendering or build, where those globals don't exist.

Likely causes

How to fix it

  1. Access browser globals inside useEffect, which only runs in the browser:

    useEffect(() => {
      const saved = window.localStorage.getItem('theme');
    }, []);
  2. Or guard the access:

    if (typeof window !== 'undefined') {
      // browser-only code
    }
  3. For browser-only libraries, load the component with next/dynamic and { ssr: false }.

Still stuck after trying this? Get it fixed for $49, or you don't pay.

Prevent this next time

Guide: catch outages before your users do.

Tools that help:

Some links on this page are affiliate links: if you sign up or buy through them, FixStack may earn a commission at no extra cost to you. Learn more.

FAQ

What does "ReferenceError: window is not defined" mean?

Code that touches a browser global (window, document, localStorage) ran on the server during server rendering or build, where those globals don't exist.

What usually causes it?

Accessing window/document at the top level of a module or during render. Importing a library that assumes a browser at import time.

How do I fix it?

1. Access browser globals inside useEffect, which only runs in the browser: 2. Or guard the access: 3. For browser-only libraries, load the component with next/dynamic and { ssr: false }.

Related errors

More Next.js errors and fixes →

Tools that help

  • Railway: Deploy backends and databases; new users get $20 credit.

Some links on this page are affiliate links: if you sign up or buy through them, FixStack may earn a commission at no extra cost to you. Learn more.

Get new fixes weekly

No spam. Unsubscribe anytime. Privacy