The Global Object in Node.js What It Is and How It Works
You have heard many times that console.log() is not a JavaScript function. but you use it in your daily development, right so have you ever wonder so it's Javascript function how we are able to use it without importing anything? When you use something from another file or the code you borrow from dependencies, you import them but you don't import the console.log why
The answer is Global Object.
What is a Global Object?
The Global Object provides us with variables, functions, & propetires that are available anywhere; it acts as a container for all globally accessible variables and functions. In simple words, it provides features built in language or the specific environment.
When you run JavaScript in a browser, there's always a window The object sitting at the top it holds everything like setTimeout, console, alert and so on. You don't import them, they're just there. That's because they live on window, which is the browser's global object.
The Nodejs have same exact concept, but in Node.js we don't have window, NodeJS creates its own global object, which we call global. Anything that lives on it. it automatically accessible from any file, module, anywhere in your app no import needed.
So when you write console.log or setTimeout In Node.js, without importing anything, it works because they are sitting on the global object behind the scenes.
What lives inside gloabl
Nodejs by default place serval built in things on the global object, like console.log, setTimeout,setInterval,process, and Buffer These tools you use every day, that Nodejs makes available everywhere without you having to ask for them.
There's also __dirname and __filename which feel global because they're always available, but they are actually not global at all. Node secretly wraps every file in a function and injects these as local variables so they belong to the file, not the global scope. This is a common misconception worth knowing.
The this confusion
in Nodejs In a browser, if you type this at the very top level of a script, it points to the window the global object. Natural and expected.
console.log(this) // window
console.log(this === window) // true
But in Node.js
You expect this at the top level to point to global. But it doesn't. It points to module.exports, which is just an empty object. This happens because Node wraps every file in a function behind the scenes (the module wrapper), so you are never truly at the top level you're always inside that wrapper function. Because of this, this behaves differently than you expect.
console.log(this === global) // false
console.log(this) // {} (module.exports)
What is globalThis
As JavaScript grew. It starts running in more environments like browsers, Node.js, Web Workers, Deno, and others. Each environment had its own name for global the object.
.Browser had
window.Node had
globalWeb Workers had
self.
This was a problem for developers writing code that needed to work across environments, because you had to write awkward checks to figure out which environment you were in before you could even reference the global object.
globalThis was introduced in ES2020 to fix this problem. It's a single standardized way to reference the global object that works exactly the same way in every JavaScript environment. No need to environment checks, no guessing, globalThis and it works everywhere.
In Node.js, globalThis and global point to the exact same object. There are two names for the same thing. But global only exists in Node while globalThis exists everywhere.
The Module Wrapper in Node.js
You read the "This happens because Node wraps every file in a function behind the scenes (the module wrapper)", but you might be wondering if Node wraps every file in a function. When you write a JavaScript file and run it with Node.js, you might think Node just takes your code and executes it as is. But that's not what actually happens. So what is that and how does it wrap.
Before Node runs your code, it takes your entire file and wraps it inside a function. You never see this happening because invisible to you, but it's always there. So whatever you write in your file, Node sees it differently internally.
What That Wrapper Actually Looks Like
The wrapper is a function that Node places around your code before executing it. Here is example how it looks like :
(function(exports, require, module, __filename, __dirname) {
// your entire file code sits here
});
Every single file in your Node.js application goes through this.
Now you understand better why the __dirname and __filename is not global.
Why Node does this
But you may be thinking, why does Node do this.
There are 2 reasons Node does this.
The first reason is isolation. By wrapping your file in a function, all the variables you declare stay scoped to that function. They don't leak out into the global scope. This is why two different files can both declare a variable with the same name, and they never conflict with each other. Each file is living inside its own function, so its variables are private to it.
The second reason is injection. By wrapping your code in a function, Node can pass in specific things as arguments to that function. This is how require, module, exports, __filename, and __dirname Get into your file. Node passes them as arguments when it calls the wrapper function.