Access the global namespace using the standard JavaScript name
Portable runtime code
Node.js · Const
globalThis is Node.js’s standard reference to the global namespace object. It is available across modules, while top-level module variables remain module-local.
globalThis is the standard JavaScript reference to the global namespace object. In Node.js, it is available in every module, while variables declared with var at module top level remain local rather than becoming properties of this object.
globalThisThe global namespace object.
globalThis refers to Node.js's global namespace object.var bindings local in both supported module systems.globalThis.var declaration is module-local in both CommonJS and ECMAScript modules.console.log(globalThis === globalThis.globalThis);
console.log(typeof globalThis.setTimeout);Run with node example.js to inspect the global namespace object and a globally available Node.js API.
Portable runtime code
Inspecting built-in globals
Intentional shared state
globalThis in Node.js?globalThis is the global namespace object exposed by Node.js. It provides a portable way to access globals without using Node-specific global.
Follow-up: How does a top-level var differ from a property assignment? A top-level var remains local to the module, whereas assigning globalThis.name creates or updates a property on the global namespace.
globalThis available in every Node.js module?It is available in all modules, including CommonJS and ECMAScript modules. The module's top-level scope is still separate from the global scope.
Follow-up: Why does this distinction matter? Module-local bindings avoid accidental process-wide sharing, while globalThis properties are shared through the global namespace.
globalThis instead of global?Use globalThis when code needs an explicit reference to the global namespace object. Prefer it over the legacy global name for standard JavaScript compatibility.
Follow-up: What should code avoid? It should not assume that top-level declarations become globals; use an explicit globalThis property only when shared global state is intentional.
globalprocessconsolesetTimeout