referenceerror: window is not defined, how to solve

Here’s how to fix the “referenceerror: window is not defined” error that you might have in Node.js or with a tool like Next.js.

window is an object that’s made available by the browser, and it’s not available in a server-side JavaScript environment.

I describe the window object is details in my extensive DOM Document Object Model guide.

With Node.js 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 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:

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.


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

Here’s how to fix the “referenceerror: window is not defined” error that you might have in Node.js or with a tool like Next.js.

window is an object that’s made available by the browser, and it’s not available in a server-side JavaScript environment.

I describe the window object is details in my extensive DOM Document Object Model guide.

With Node.js 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 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:

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.


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2022-04-24T05:00:00+00:00) referenceerror: window is not defined, how to solve. Retrieved from https://www.scien.cx/2022/04/24/referenceerror-window-is-not-defined-how-to-solve/

MLA
" » referenceerror: window is not defined, how to solve." flaviocopes.com | Sciencx - Sunday April 24, 2022, https://www.scien.cx/2022/04/24/referenceerror-window-is-not-defined-how-to-solve/
HARVARD
flaviocopes.com | Sciencx Sunday April 24, 2022 » referenceerror: window is not defined, how to solve., viewed ,<https://www.scien.cx/2022/04/24/referenceerror-window-is-not-defined-how-to-solve/>
VANCOUVER
flaviocopes.com | Sciencx - » referenceerror: window is not defined, how to solve. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/04/24/referenceerror-window-is-not-defined-how-to-solve/
CHICAGO
" » referenceerror: window is not defined, how to solve." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2022/04/24/referenceerror-window-is-not-defined-how-to-solve/
IEEE
" » referenceerror: window is not defined, how to solve." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2022/04/24/referenceerror-window-is-not-defined-how-to-solve/. [Accessed: ]
rf:citation
» referenceerror: window is not defined, how to solve | flaviocopes.com | Sciencx | https://www.scien.cx/2022/04/24/referenceerror-window-is-not-defined-how-to-solve/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.