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 onglobalThis, 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 |
|---|---|
| Logging and diagnostics → The console Module |
| Current process info & control → The process Object |
| Binary data (pre-dates typed arrays in Node) |
| Schedule code later / repeatedly → Timers |
| Run after the current poll phase |
| Schedule a microtask |
| HTTP requests (Web standard) |
| Parse and build URLs |
| Encode/decode text ⇄ bytes |
| Deep-clone objects |
| Cancel async operations |
| Web Crypto: |
| High-resolution timing ( |
Module-scoped pseudo-globals (CommonJS)
Name | Purpose | ESM replacement |
|---|---|---|
| Absolute path of the current directory |
|
| Absolute path of the current file |
|
| Import a module synchronously |
|
| Reference to the current module |
|
| Shorthand for |
|
ESM equivalents in one place
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 ESMTimers at a glance
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:
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
processandBufferas 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).