Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Node.js · Const

globalThis

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.

How it works

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.

Syntax

globalThis

Returns

The global namespace object.

Key points

  • globalThis refers to Node.js's global namespace object.
  • The global object and its documented built-ins can be accessed from every module.
  • The browser model historically treats the top-level scope as global, unlike Node.js modules.
  • Node.js keeps module top-level var bindings local in both supported module systems.

Common mistakes

  • Declaring a top-level variable in a Node.js module does not automatically add it to globalThis.
  • A top-level var declaration is module-local in both CommonJS and ECMAScript modules.

Example: Inspect the global namespace

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.

When to use it

Access the global namespace using the standard JavaScript name

Portable runtime code

Check whether a globally available API exists

Inspecting built-in globals

Expose a value globally with an explicit property assignment

Intentional shared state

Interview questions

QWhat is 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.

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

QWhy use 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.

  • global
  • process
  • console
  • setTimeout

Source: Global objects | Node.js v26.11.1 Documentation. Checked 2026-10-15.