Skip to main content

Global Objects

These objects are available in all modules. Some of these objects aren’t actually in the global scope but in the module scope.

Global Variables

The following variables may appear to be global but exist only in the scope of CommonJS modules:
  • __dirname
  • __filename
  • exports
  • module
  • require()

Class: AbortController

Added in: v15.0.0, v14.17.0
A utility class used to signal cancelation in selected Promise-based APIs. The API is based on the Web API AbortController.
const ac = new AbortController();

ac.signal.addEventListener('abort', () => console.log('Aborted!'), { once: true });

ac.abort();

console.log(ac.signal.aborted);  // Prints true

abortController.abort([reason])

reason
any
An optional reason, retrievable on the AbortSignal’s reason property.
Triggers the abort signal, causing the abortController.signal to emit the ‘abort’ event.

abortController.signal

signal
AbortSignal
The AbortSignal object associated with this AbortController.

Class: AbortSignal

Extends: EventTarget The AbortSignal is used to notify observers when the abortController.abort() method is called.

Static Methods

AbortSignal.abort([reason])

reason
any
Optional reason for aborting.
Returns a new already aborted AbortSignal.

AbortSignal.timeout(delay)

delay
number
required
The number of milliseconds to wait before triggering the AbortSignal.
Returns a new AbortSignal which will be aborted in delay milliseconds.

AbortSignal.any(signals)

signals
AbortSignal[]
required
The AbortSignals of which to compose a new AbortSignal.
Returns a new AbortSignal which will be aborted if any of the provided signals are aborted.

Event: ‘abort’

The ‘abort’ event is emitted when the abortController.abort() method is called.
const ac = new AbortController();

ac.signal.addEventListener('abort', (event) => {
  console.log(event.type);  // Prints 'abort'
}, { once: true });

ac.abort();

abortSignal.aborted

aborted
boolean
True after the AbortController has been aborted.

abortSignal.reason

reason
any
An optional reason specified when the AbortSignal was triggered.
const ac = new AbortController();
ac.abort(new Error('boom!'));
console.log(ac.signal.reason);  // Error: boom!

abortSignal.throwIfAborted()

If abortSignal.aborted is true, throws abortSignal.reason.

Class: Buffer

Added in: v0.1.103
Used to handle binary data. See the Buffer section.

console

console
Object
Used to print to stdout and stderr. See the Console section.

fetch

Added in: v17.5.0, v16.15.0. No longer experimental in v21.0.0.
A browser-compatible implementation of the fetch() function.
const res = await fetch('https://nodejs.org/api/documentation.json');
if (res.ok) {
  const data = await res.json();
  console.log(data);
}
The implementation is based upon undici, an HTTP/1.1 client written for Node.js. The following globals are available to use with fetch:
  • FormData
  • Headers
  • Request
  • Response

global

Stability: 3 - Legacy. Use globalThis instead.
global
Object
The global namespace object.
In browsers, the top-level scope is the global scope. In Node.js, the top-level scope is not the global scope; var something inside a Node.js module will be local to that module.

performance

Added in: v16.0.0
The perf_hooks.performance object.

process

Added in: v0.1.7
process
Object
The process object. See the Process section.

queueMicrotask(callback)

Added in: v11.0.0
callback
Function
required
Function to be queued.
The queueMicrotask() method queues a microtask to invoke callback. If callback throws an exception, the process object 'uncaughtException' event will be emitted. The microtask queue is managed by V8 and may be used in a similar manner to the process.nextTick() queue, which is managed by Node.js. The process.nextTick() queue is always processed before the microtask queue within each turn of the Node.js event loop.
queueMicrotask(() => {
  console.log('Microtask executed');
});

Timer Functions

setImmediate(callback[, …args])

Added in: v0.9.1
callback
Function
required
The function to call at the end of this turn of the event loop.
...args
any
Optional arguments to pass when the callback is called.
See the Timers section.

setInterval(callback, delay[, …args])

Added in: v0.0.1
callback
Function
required
The function to call when the timer elapses.
delay
number
The number of milliseconds to wait before calling the callback. Default: 1.
See the Timers section.

setTimeout(callback, delay[, …args])

Added in: v0.0.1
callback
Function
required
The function to call when the timer elapses.
delay
number
The number of milliseconds to wait before calling the callback. Default: 1.
See the Timers section.

clearImmediate(immediateObject)

Added in: v0.9.1
Cancels an Immediate object created by setImmediate().

clearInterval(intervalObject)

Added in: v0.0.1
Cancels a Timeout object created by setInterval().

clearTimeout(timeoutObject)

Added in: v0.0.1
Cancels a Timeout object created by setTimeout().

URL

Added in: v10.0.0
The WHATWG URL class.

URLSearchParams

Added in: v10.0.0
The WHATWG URLSearchParams class.

WebAssembly

Added in: v8.0.0
WebAssembly
Object
The object that acts as the namespace for all W3C WebAssembly related functionality.