Node.js global objects are values and functions—such as console, process, Buffer, setTimeout, URL, and fetch—available inside every module without an import or require(). Understanding what is truly global versus module-scoped prevents bugs when moving between CommonJS, ES Modules, and browser JavaScript.
Modern Node.js aligns closely with browser globals through globalThis, while adding server-only globals for binary buffers and process control.
| Global | Scope | Purpose |
|---|---|---|
globalThis |
All JS runtimes | Standard reference to the global object |
process |
Node.js | Environment variables, CLI args, exit codes |
Buffer |
Node.js | Raw binary byte handling |
fetch / URL |
Node 18+ & Browser | HTTP requests and URL parsing |
setTimeout / queueMicrotask |
Node.js & Browser | Scheduling timers and microtasks |
__dirname / __filename |
CommonJS only | Current file's directory and path |
console.log('Using globalThis === global:', globalThis === global);
console.log('Node version:', process.version);
const encoded = Buffer.from('koder').toString('hex');
console.log('Hex via global Buffer:', encoded);
queueMicrotask(() => console.log('2. Microtask runs before timers'));
setTimeout(() => console.log('3. Timer callback runs next'), 0);
console.log('1. Synchronous top-level line');
__dirname and require Are Not Global in ESMEven in CommonJS, __dirname, __filename, exports, module, and require are technically module-wrapper parameters injected per file—not true properties on globalThis. In ES Modules ("type": "module"), those five wrappers disappear; use import.meta.dirname and import.meta.filename (Node 20.11+) instead.
global.db = ...: Mutating globalThis creates hidden coupling and race conditions in tests. Export shared singletons cleanly from a module instead.window or localStorage in Node.js: Browser DOM globals throw ReferenceError: window is not defined on the server—especially inside Next.js Server Components.setTimeout(..., 0) without cleaning up on shutdown: Long-lived intervals (setInterval) keep the Node.js event loop alive and prevent graceful shutdown unless cleared with clearInterval() or .unref().globalThis over global?globalThis is part of the ECMAScript standard and works identically in Node.js, browsers, Deno, and Cloudflare Workers, whereas global only exists in Node.js.
structuredClone() global in Node.js?Yes. Node.js 17+ includes global structuredClone(obj) for deep-cloning nested objects, arrays, Maps, Sets, and Dates without JSON.parse(JSON.stringify()) hacks.
setImmediate() differ from setTimeout(fn, 0)?In Node.js, setImmediate() executes in the Check phase right after the current I/O poll phase finishes, making its order deterministic inside I/O callbacks.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.
You've completed this section! Take a quick 5-question quiz to check your understanding.