Node.js Reference ยท Globals and Console
Node.js globalThis
Reference for globalThis in Node.js: the standard global object, what lives on it, how it differs from window and global, and why to avoid adding to it.
globalThis is the standard name for the global object in every JavaScript environment. In Node.js it holds the names that are available in every file without an import: console, process, Buffer, setTimeout, fetch, URL, structuredClone and the built-in constructors such as Array and Promise.
Browsers call the same object window (or self in workers). Node has no window, so globalThis is the portable spelling. Node also keeps the older alias global, which points to the same object.
console.log(globalThis === global); // true console.log(typeof globalThis.setTimeout); // function
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Syntax
globalThis
globalThis is a read-only binding to the global object; you cannot reassign it. There is no call syntax: you read properties from it or write new ones.
| Item | Description |
|---|---|
globalThis.name |
Reads or sets a property that every module can see as a bare identifier name. |
global |
Node-specific alias of globalThis. Older code uses it. |
window, self, document |
Not defined in Node. Use 'window' in globalThis to feature-detect a browser. |
Returns: the global object.
Edge cases: reading an undeclared bare name throws ReferenceError; typeof name returns "undefined" instead.
Usage notes
- Module scope is not global scope. In an ES module (and in CommonJS, which wraps files in a function)
var,let,constand function declarations stay private to the file. Only a deliberateglobalThis.x = ...creates a global. - Prefer
globalThisoverglobal. It works unchanged in browsers, Deno, Bun and workers, so shared code needs no environment check. - Detect environments by feature, not by name.
typeof window === 'undefined'and'window' in globalThisboth work; checking for a specific API (typeof fetch) is more future-proof. - Built-in globals are mostly non-enumerable.
Object.keys(globalThis)shows only a few names; useObject.getOwnPropertyNames(globalThis)for the full list. - Avoid adding your own globals. Any file can overwrite them, nothing documents where they come from, and tests leak state into each other. Export from a module instead. A rare legitimate use is a polyfill that fills in a missing standard API.
- Globals added by Node itself differ by version.
fetch,structuredCloneandnavigatorexist on Node 24; do not assume a newer API is present on an older runtime. require,module,exports,__dirname,__filenameare not onglobalThis. They are parameters of the CommonJS wrapper function, so they exist only in CommonJS files.- Workers have their own global object. Each
worker_threadsworker gets a separateglobalThis; setting a property in one thread does not affect the others.