Welcome to the new Web Developer's Journal built with Nuxt Content!

Node global vs. window Object

You know how when you declare a variable with var in a browser, it will be added to the window object.

var myVar = 'Hi';

console.log(myVAr === window.myVar); // true

That's not the case with the global object.

console.log(myVAr === global.myVar); // false

In node, every file is a module. If you want to use a variable outside a module, you have to export it and then import it.

You have the same objects and methods as in the window object, but you also get some extra tools. The most used are probably __dirname and __filename, along with the require() function. For a full list of global objects, you can consult the official documentation.