The global object global
1. What global is in Node.js
globalis Node.js's global object, present in every module, holding globally available variables and functions visible from anywhere in the program.
Its counterpart in other environments:
| Environment | Global object |
|---|---|
| Node.js | global |
| Browser | window |
| Web Workers | self |
| Modern standard | globalThis |
2. The core idea
When you run a Node.js application, it runs in its own process and environment.
Some objects (for example, process, Buffer, console) need to be available in any module,
so they're defined as properties of the global object.
3. Example
console.log(global); // prints every global propertySome key properties:
{
global: [Circular],
process: [process],
console: [console],
setTimeout: [Function],
clearTimeout: [Function],
setInterval: [Function],
Buffer: [class Buffer],
...
}4. An important difference from the browser
| Feature | In the browser | In Node.js |
|---|---|---|
| Global object | window | global |
| Global functions (setTimeout, console, etc.) | Properties of window | Properties of global |
Declaring var at the global scope | adds to window | does not add to global |
this at the top level | this === window | this !== global |
5. A check:
console.log(this === global); // falseWhy? In Node.js, every file is a module, wrapped in a function:
(function(exports, require, module, __filename, __dirname) {
// your code
});So this inside a file refers to module.exports,
not to global.
6. Accessing global objects
You can use global functions without importing them:
setTimeout(() => console.log('Hello!'), 1000);
console.log(process.pid);
console.log(Buffer.from('Hi!'));Under the hood, these are all properties of global:
global.setTimeout(() => console.log('Hello!'), 1000);
global.process;
global.Buffer;7. Commonly used global properties
| Property | Purpose |
|---|---|
global | A reference to the global object itself |
process | Information about the process and the Node.js environment |
console | The global logger |
Buffer | Working with binary data |
setTimeout() / setInterval() / setImmediate() | Timers |
clearTimeout() / clearInterval() / clearImmediate() | Clearing timers |
queueMicrotask() | Adding a microtask to the Event Loop |
globalThis | The standard ES2020 global object |
performance | Performance metrics (from Node 8.5+) |
8. Creating your own global variables
You can add your own values to global (though it's not recommended!):
global.APP_NAME = 'MyApp';
global.VERSION = '1.0.0';
console.log(APP_NAME); // MyAppIt works in any module:
// app.js
global.APP_NAME = 'ShopX';
// routes.js
console.log(APP_NAME); // ShopXBut this is bad practice, because it:
- creates hidden dependencies between modules;
- makes testing and debugging harder;
- can cause naming conflicts.
It's better to export data explicitly:
// config.js
export const APP_NAME = 'ShopX';9. The difference between global and globalThis
| Object | Where it's available | Note |
|---|---|---|
global | Node.js only | Node-specific |
window | Browser only | Browser-specific |
globalThis | Universal | Works in both Node.js and the browser |
Example:
console.log(globalThis === global); // true in Node.js
console.log(globalThis === window); // true in a browser10. A practical example
Checking the environment:
if (global.process) {
console.log('The code is running in Node.js');
}Creating a global variable with a safe check:
if (!globalThis.config) {
globalThis.config = { env: 'development' };
}
console.log(globalThis.config.env); // development11. Visually
┌────────────────────────────────┐
│ Node.js Runtime │
├────────────────────────────────┤
│ global │
│ ├─ process │
│ ├─ console │
│ ├─ setTimeout() │
│ ├─ Buffer │
│ ├─ ... │
│ └─ (your variables, if added) │
└────────────────────────────────┘12. Quick summary
| Point | Description |
|---|---|
| What it is | Node.js's top-level global object |
| Purpose | Holds functions and variables available from any module |
| Main properties | process, console, Buffer, setTimeout, setImmediate, and others |
| Browser counterpart | window |
| Modern standard | globalThis |
| Recommendation | Don't clutter global with your own variables, use imports/exports instead |
In one sentence:
globalis Node.js's top-level object that makes system functions and variables (for example,process,Buffer,setTimeout) available in every module without an import, but it is not the scope forthisorvar.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.