The process Object
process is a global object that represents — and lets you control — the currently running Node.js process. It is your window into the environment, command-line arguments, standard I/O streams, lifecycle events, resource usage, and the exit code. You will reach for it constantly, and several of its members are important enough to have their own pages. This page is the map of the whole object.
Identity & runtime information
process.pid // process ID, e.g. 48213
process.ppid // parent process ID
process.platform // 'darwin' | 'linux' | 'win32'
process.arch // 'x64' | 'arm64'
process.version // Node version string, e.g. 'v20.11.0'
process.versions // { node, v8, uv, openssl, ... }
process.cwd() // current working directory
process.uptime() // seconds since the process started
process.execPath // absolute path to the node binary
process.title // process name shown in 'ps' / Task Managerpid 48213 on linux/x64, Node v20.11.0, up 0.07s
The members you will use most
Member | What it gives you |
|---|---|
| Environment variables → process.env |
| Command-line arguments → process.argv |
| Standard I/O streams |
| Terminate immediately with an exit code |
| Set the code without forcing an immediate exit |
| Subscribe to lifecycle events & signals → Process Events |
| Run a callback before the next loop phase → nextTick |
| Heap and RSS memory statistics |
| User/system CPU time consumed |
| High-resolution nanosecond timer |
Standard streams
process.stdout and process.stderr are writable streams; process.stdin is a readable stream. In fact console.log is essentially process.stdout.write with formatting and a trailing newline, and console.error writes to stderr. Writing directly gives you control over newlines and lets you build pipelines.
Reading piped input — echo 'hi there' | node app.js
process.stdout.write('Type something and press Enter:\n')
process.stdin.setEncoding('utf8')
process.stdin.on('data', (chunk) => {
process.stdout.write(`You typed: ${chunk}`)
})
process.stdin.on('end', () => process.stdout.write('Input closed.\n'))Because they are streams, they also expose whether they are connected to a terminal via .isTTY — useful for deciding whether to print colours.
Exit codes and exiting cleanly
A process ends with an exit code: 0 means success, any non-zero value means failure (CI systems and shells rely on this). There are two ways to set it, and the difference matters:
// PREFERRED: signal failure but let pending I/O drain naturally. process.exitCode = 1 // ...the process exits with code 1 once the event loop empties. // FORCEFUL: terminate right now. Pending writes/logs may be lost. process.exit(1)
Measuring resources and time
Benchmark a block of work
const startCpu = process.cpuUsage()
const start = process.hrtime.bigint()
doExpensiveWork()
const ns = process.hrtime.bigint() - start
const cpu = process.cpuUsage(startCpu) // delta since startCpu
console.log(`Wall time: ${Number(ns) / 1e6} ms`)
console.log(`CPU user: ${cpu.user / 1000} ms, system: ${cpu.system / 1000} ms`)
const m = process.memoryUsage()
console.log(`Heap used: ${(m.heapUsed / 1e6).toFixed(1)} MB`)Lifecycle events (preview)
process is an EventEmitter, so you can subscribe to important moments — exit, beforeExit, uncaughtException, unhandledRejection, and OS signals like SIGTERM/SIGINT. These power graceful shutdown and last-resort error handling, covered fully in Process Events & Signals.
process.on('exit', (code) => {
// Synchronous cleanup only — the loop is already stopping.
console.log(`Exiting with code ${code}`)
})