Lesson

Node.js Global Objects

10 min reading
Free Course

Node.js Global Objects: What Is Built Into Every Module

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.

Standard Cross-Platform Globals vs Node-Specific Globals

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');

Why __dirname and require Are Not Global in ESM

Even 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.

Common Mistakes to Avoid

  • Attaching application state to global.db = ...: Mutating globalThis creates hidden coupling and race conditions in tests. Export shared singletons cleanly from a module instead.
  • Expecting window or localStorage in Node.js: Browser DOM globals throw ReferenceError: window is not defined on the server—especially inside Next.js Server Components.
  • Using 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().

Frequently Asked Questions

Why prefer 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.

Is 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.

How does 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.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
Lesson Recap Quiz Available

Test Your Knowledge

You've completed this section! Take a quick 5-question quiz to check your understanding.

Node.js Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum