Skip to main content

The global object global

1. What global is in Node.js

global is Node.js's global object, present in every module, holding globally available variables and functions visible from anywhere in the program.

Its counterpart in other environments:

EnvironmentGlobal object
Node.jsglobal
Browserwindow
Web Workersself
Modern standardglobalThis

2. The core idea

When you run a Node.js application, it runs in its own process and environment. Some objects (for example, process, Buffer, console) need to be available in any module, so they're defined as properties of the global object.

3. Example

javascript
console.log(global); // prints every global property

Some key properties:

javascript
{ global: [Circular], process: [process], console: [console], setTimeout: [Function], clearTimeout: [Function], setInterval: [Function], Buffer: [class Buffer], ... }

4. An important difference from the browser

FeatureIn the browserIn Node.js
Global objectwindowglobal
Global functions (setTimeout, console, etc.)Properties of windowProperties of global
Declaring var at the global scopeadds to windowdoes not add to global
this at the top levelthis === windowthis !== global

5. A check:

javascript
console.log(this === global); // false

Why? In Node.js, every file is a module, wrapped in a function:

javascript
(function(exports, require, module, __filename, __dirname) { // your code });

So this inside a file refers to module.exports, not to global.

6. Accessing global objects

You can use global functions without importing them:

javascript
setTimeout(() => console.log('Hello!'), 1000); console.log(process.pid); console.log(Buffer.from('Hi!'));

Under the hood, these are all properties of global:

javascript
global.setTimeout(() => console.log('Hello!'), 1000); global.process; global.Buffer;

7. Commonly used global properties

PropertyPurpose
globalA reference to the global object itself
processInformation about the process and the Node.js environment
consoleThe global logger
BufferWorking with binary data
setTimeout() / setInterval() / setImmediate()Timers
clearTimeout() / clearInterval() / clearImmediate()Clearing timers
queueMicrotask()Adding a microtask to the Event Loop
globalThisThe standard ES2020 global object
performancePerformance metrics (from Node 8.5+)

8. Creating your own global variables

You can add your own values to global (though it's not recommended!):

javascript
global.APP_NAME = 'MyApp'; global.VERSION = '1.0.0'; console.log(APP_NAME); // MyApp

It works in any module:

javascript
// app.js global.APP_NAME = 'ShopX'; // routes.js console.log(APP_NAME); // ShopX

But this is bad practice, because it:

  • creates hidden dependencies between modules;
  • makes testing and debugging harder;
  • can cause naming conflicts.

It's better to export data explicitly:

javascript
// config.js export const APP_NAME = 'ShopX';

9. The difference between global and globalThis

ObjectWhere it's availableNote
globalNode.js onlyNode-specific
windowBrowser onlyBrowser-specific
globalThisUniversalWorks in both Node.js and the browser

Example:

javascript
console.log(globalThis === global); // true in Node.js console.log(globalThis === window); // true in a browser

10. A practical example

Checking the environment:

javascript
if (global.process) { console.log('The code is running in Node.js'); }

Creating a global variable with a safe check:

javascript
if (!globalThis.config) { globalThis.config = { env: 'development' }; } console.log(globalThis.config.env); // development

11. Visually

javascript
┌────────────────────────────────┐ Node.js Runtime├────────────────────────────────┤ │ global │ │ ├─ process │ │ ├─ console │ │ ├─ setTimeout()│ ├─ Buffer│ ├─ ...└─ (your variables, if added)└────────────────────────────────┘

12. Quick summary

PointDescription
What it isNode.js's top-level global object
PurposeHolds functions and variables available from any module
Main propertiesprocess, console, Buffer, setTimeout, setImmediate, and others
Browser counterpartwindow
Modern standardglobalThis
RecommendationDon't clutter global with your own variables, use imports/exports instead

In one sentence:

global is Node.js's top-level object that makes system functions and variables (for example, process, Buffer, setTimeout) available in every module without an import, but it is not the scope for this or var.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.