What are the global objects in Node, and how do `global`, `process`, and `globalThis` relate?
Quick Answer
`global` is Node's global object (the browser's `window` equivalent); `globalThis` is the standard, environment-agnostic reference to it. `process`, `Buffer`, `setTimeout`, and `console` are available globally. Note that top-level variables in a CommonJS module are module-scoped, not global.
Detailed Answer
Answer:
global — Node's global namespace object, analogous to window in the browser. Properties attached to it are visible everywhere:
global.myShared = 42; // accessible as myShared anywhere (generally avoid this)
globalThis — the ES2020 standard way to reference the global object regardless of environment. In Node it is global; in the browser it's window. Prefer globalThis for portable code.
process — a specific global object (not the global scope itself) describing the current process; see environment variables, argv, signals, etc.
Globals you can use without require:
console,setTimeout/setInterval/setImmediate,queueMicrotaskBuffer,TextEncoder/TextDecoder,URL,fetch(modern Node)__dirname,__filename,require,module,exports(in CommonJS)
Important subtlety — module scope vs global scope:
// In a CommonJS file:
const x = 1; // module-scoped, NOT a property of global
global.y = 2; // truly global
console.log(global.x); // undefined
console.log(global.y); // 2
Each file is wrapped in a function by Node, so top-level const/let/var stay local to that module. This is why one file's variables don't leak into another — a common point of confusion for people coming from browser <script> tags where top-level var is global.
Note: __dirname and __filename exist in CommonJS but not in ES Modules, where you derive them from import.meta.url.