NodeJSGlobal Variables & Functions

Global Variables & Functions

Some names are available in every Node.js file without an import or require. This page is a practical, grouped catalogue of those globals — what each does, whether it is truly global or merely module-scoped, and the cross-environment gotchas. Think of it as the reference you scan when you see an unfamiliar bare identifier and wonder "where did that come from?".

Three kinds of "global"

Not everything that works without an import is the same kind of thing. There are three categories, and confusing them causes real bugs:

  • Truly global — properties of globalThis, shared across the whole process (console, process, fetch).

  • Module-scoped pseudo-globals — injected per-file by the CommonJS wrapper (__dirname, require); not on globalThis, and absent in ESM.

  • Language built-ins — part of JavaScript itself, available in any runtime (Math, JSON, Array, Promise).

Truly global (on globalThis)

Global

Purpose

console

Logging and diagnostics → The console Module

process

Current process info & control → The process Object

Buffer

Binary data (pre-dates typed arrays in Node)

setTimeout / setInterval

Schedule code later / repeatedly → Timers

setImmediate

Run after the current poll phase

queueMicrotask

Schedule a microtask

fetch

HTTP requests (Web standard)

URL / URLSearchParams

Parse and build URLs

TextEncoder / TextDecoder

Encode/decode text ⇄ bytes

structuredClone

Deep-clone objects

AbortController / AbortSignal

Cancel async operations

crypto

Web Crypto: randomUUID(), subtle, getRandomValues()

performance

High-resolution timing (performance.now())

Module-scoped pseudo-globals (CommonJS)

Name

Purpose

ESM replacement

__dirname

Absolute path of the current directory

dirname(fileURLToPath(import.meta.url))

__filename

Absolute path of the current file

fileURLToPath(import.meta.url)

require

Import a module synchronously

import (or createRequire)

module

Reference to the current module

import.meta

exports

Shorthand for module.exports

export statements

Why they are not on globalThis
These are parameters of the function the [module wrapper](/nodejs/module-wrapper) wraps your file in — so each module gets its *own* values. `global.__dirname` is `undefined`. In ES modules they are not provided at all; use `import.meta.url`.

ESM equivalents in one place

JS
import { fileURLToPath } from 'node:url'
import { dirname } from 'node:path'
import { createRequire } from 'node:module'

const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
const require = createRequire(import.meta.url)  // if you must use require in ESM
Timers at a glance

JS
const id = setTimeout(() => console.log('once, later'), 1000)
clearTimeout(id)

const tick = setInterval(() => console.log('every second'), 1000)
clearInterval(tick)

const imm = setImmediate(() => console.log('after I/O, before timers'))
clearImmediate(imm)

queueMicrotask(() => console.log('microtask — runs very soon'))

Their precise ordering is the event loop's job — see Timers and setImmediate & process.nextTick.

Buffer — Node's binary type

Buffer represents fixed-length raw bytes (file contents, network packets, binary protocols). It is a global subclass of Uint8Array:

JS
const buf = Buffer.from('héllo', 'utf8')
console.log(buf)              // <Buffer 68 c3 a9 6c 6c 6f>
console.log(buf.length)      // 6 bytes (é is two bytes in UTF-8)
console.log(buf.toString('hex'))   // '68c3a96c6c6f'

Buffers get a full treatment in Buffers.

Rules of thumb
  • Use language/web-standard globals freely (console, fetch, URL, crypto) — they are portable.

  • Treat process and Buffer as Node-specific — fine on the server, not in code shared with the browser.

  • Remember CommonJS pseudo-globals vanish in ESM — reach for import.meta.url.

  • Never add your own properties to globalThis; share via modules instead (see The global Object).

Next
Dive into [The process Object](/nodejs/process-object) — the gateway to environment, arguments, streams, and lifecycle.