. Advertisement .
..3..
. Advertisement .
..4..
Any project should have a section dedicated to handling errors. Many of you, when participating in the project, do not know how to handle errors in a scientific way. If handled correctly, it reduces application development time, making the code base easily extensible. For those of you who are new to Node.JS, you will certainly feel that Node.JS is difficult to learn and work because it is too messy. In this article, I will introduce how to handle errors in NodeJS in a scientific, clean code.Therefore, today we will show how to handle when you get this error “ReferenceError: window is not defined at object” in Node.js
When does it occur?
Surely anyone who has ever learned programming has also heard of the concept of global scope, typically when we write Javascript to run in the browser, it provides us with a global object called window. We can call window anywhere without declaration.
NodeJS provides us with a few global objects (global Objects) useful in the process of working with NodeJS. you should remember, nodejs doesn’t have window.
In this example you are having a program with variables already declared and stored in client.js file like below
window.windowVar= {
func1: function(args) {
//some sode here
},
counter:0
};
Similarly you also create another program in myModule.js file to output it and request it via server.js file
module.exports={wVar:windowVar, addMessage ,getMessages, deleteMessage};
windowVar.serverCounter = 0;
windowVar.arr1=[];
This is the command you use in the server.js . file
var m= require('./myModule');
But then you got an error message:
ReferenceError : window is not defined at object. <anonymous>
How to solve “ReferenceError: window is not defined at object”
When you get the “ReferenceError: window is not defined at object” error, there are many options for you to select the best solution to fix the problem below:
Option 1: Use globalThis
or (for older versions) global
// but do not do thi, keep reading
globalThis.windowVar = /*...*/:
// or
global.windowVar = /*...*/;
global
is the identifier used by Node for an object that is global (defined within their API prior to globalThis
was created) as windows
is in browsers. It is used for code that can run in a variety of environments, such as older ones, like below:
const g = typeof globalThis === "object"
? globalThis
: typeof window === "object"
? window
: typeof global === "object"
? global
: null; // Causes an error on the next line
g.windowVar = /*...*/;
or you can create a module global as follow
let /*or `const`*/ windowVar = /*...*/;
Option 2 : Do this command
Follow this command to solve the problem
let foo = null;
if (typeof window !== "undefined") {
foo = window.localStorage.getItem("foo");
}
Option 3: Use require
and exports
When using the application on nodejs
no window
object is available, window
object is present only in the context of browser. If you want to share the variables/ functions across multiple files, you need run require
& exports
With client.js
module.exports = {
fun1: function(){
},
counter: 0
}
With myModule.js
var client = require('./client');
Conclusion
Some Node.js issues can have a devastating effect on your program. Although Node.js has a lot of support for beginners, there are still some places where there is a problem. Through the article “How to solve “ReferenceError: window is not defined at object” in Node.js” we hope this short tutorial will help beginners to write better Node.js code and develop good software more stable and convenient.
If you have any difficulty, leave us a message to find a solution soon and hope you have a enjoyable program. Thanks for reading!
1 Comment