close

[Solved] ReferenceError : window is not defined at object. Node.js

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error ReferenceError : window is not defined at object. Node.js in javascript. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How ReferenceError : window is not defined at object. Node.js Error Occurs?

Today I get the following error ReferenceError : window is not defined at object. Node.js in javascript.

How To Solve ReferenceError : window is not defined at object. Node.js Error ?

  1. How To Solve ReferenceError : window is not defined at object. Node.js Error ?

    To Solve ReferenceError : window is not defined at object. Node.js Error I used something like this and it protects against the error:
    let foo = null; if (typeof window !== "undefined") { foo = window.localStorage.getItem("foo"); }

  2. ReferenceError : window is not defined at object. Node.js

    To Solve ReferenceError : window is not defined at object. Node.js Error I used something like this and it protects against the error:
    let foo = null; if (typeof window !== "undefined") { foo = window.localStorage.getItem("foo"); }

Solution 1

window is a browser thing that doesn’t exist on Node.

If you really want to create a global, use global instead:

global.windowVar = /*...*/; // BUT PLEASE DON'T DO THIS, keep reading

global is Node’s identifier for the global object, like window is on browsers.

But, there’s no need to create truly global variables in Node programs. Instead, just create a module global:

var windowVar = /*...*/;

…and since you include it in your exports, other modules can access the object it refers to as necessary.

Solution 2

I used something like this and it protects against the error:

let foo = null;
if (typeof window !== "undefined") {
  foo = window.localStorage.getItem("foo");
}

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also Read

Leave a Comment