# referenceerror: window is not defined, how to solve 

> Learn how to fix the referenceerror: window is not defined error in Node.js or Next.js by guarding browser-only code with a typeof window check first.

Author: [Flavio Copes](https://flaviocopes.com/about/) | Published: 2022-04-24 | Topics: [Node.js](https://flaviocopes.com/tags/node/) | Canonical: https://flaviocopes.com/how-to-fix-referenceerror-window-not-defined/

Here's how to fix the "referenceerror: window is not defined" error that you might have in [Node.js](https://flaviocopes.com/nodejs/) or with a tool like [Next.js](https://flaviocopes.com/nextjs/).

`window` is an object that's made available by the browser, and it's not available in a server-side [JavaScript](https://flaviocopes.com/javascript/) environment.

I describe the `window` object is details in my extensive [DOM Document Object Model guide](https://flaviocopes.com/dom/).

With [Node.js](https://flaviocopes.com/nodejs/) in particular there's no way to workaround the problem - you must find the particular place where `window` is used, and revisit the code to figure out *why* you are accessing the `window` object.

You are running frontend code in a backend environment.

In [Next.js](https://flaviocopes.com/nextjs/) you can fix this problem by wrapping the code you run in a conditional.

The code might be running in both situations - frontend, when you navigate to a page using a link, and server-side if you require server-side into your page, for example by running `getServerSideProps()`.

In this case, you can limit the reference into a conditional that checks if the `window` object is available, like this:

```js
if (typeof window !== 'undefined') {
  //here `window` is available
}
```

And this will fix your problem, since you only run anything inside the conditional in a browser environment.
