ErrorBellFree

Error library / this error

How to fix it

ReferenceError: window is not defined

What it means

Code that needs the browser ran on the server, where there is no window, document or localStorage.

Common causes

  • Server-side rendering (Next.js, Nuxt, Remix) runs your component in Node first.
  • A library touches window when it is imported.

How to fix it

1. Move browser-only code into an effect

Effects only run in the browser.

useEffect(() => {
  setWidth(window.innerWidth);
}, []);

2. Guard with a check

if (typeof window !== 'undefined') { /* browser only */ }

3. Load the library on the client only

In Next.js, use a dynamic import with SSR turned off.

const Chart = dynamic(() => import('./Chart'), { ssr: false });

Catch it the moment it happens

ErrorBell shows a dot on the page when this error occurs and takes you to the failing line, so you do not have to hunt through the console. Add it to Chrome, it is free.

Related errors