process

The process module exposes information and utilities about the current running program: IDs, arguments, environment variables, working directory, performance/memory, exit codes, signals, and lifecycle events.


API Surface

Properties

  • process.pid: numeric process ID.
  • process.ppid: parent process ID.
  • process.argv: array of command‑line arguments (argv[0] = runtime, argv[1] = script).
  • process.execPath: absolute path to the runtime executable.
  • process.cwd(): current working directory string.
  • process.env: mutable map of environment variables (strings).
  • process.platform: OS identifier (e.g., win32, linux, darwin).
  • process.arch: CPU architecture (x64, arm64, etc.).
  • process.uptime(): seconds since start.
  • process.hrtime.bigint(): high‑resolution time in nanoseconds as bigint.
  • process.memoryUsage(): object with RSS/heap stats.
  • process.exitCode: number used when process exits (defaults to 0).

Methods

  • process.chdir(path): change working directory.
  • process.exit([code]): exit immediately (use sparingly; prefer letting the event loop drain).
  • process.kill(pid[, signal]): send a signal to a PID (default SIGTERM).
  • process.nextTick(fn): queue a microtask to run before other queued work.
  • setImmediate(fn): schedule a macrotask after I/O callbacks (paired with clearImmediate(id)).

Events

  • 'beforeExit': emitted when the event loop becomes empty. You may schedule more work here.
  • 'exit' (code): the process is exiting; only synchronous work allowed.
  • 'uncaughtException' (err): an exception bubbled to the top.
  • 'unhandledRejection' (reason, promise): a Promise rejected without a handler.
  • 'warning' (warning): non‑fatal runtime warnings.
  • Signal events: e.g., 'SIGINT', 'SIGTERM', 'SIGHUP'.

Signals (common)

  • SIGINT: interrupt (Ctrl+C).
  • SIGTERM: polite termination request.
  • SIGHUP: terminal/session closed; often used to reload config.
  • SIGKILL: forced kill (cannot be caught or ignored).
  • SIGUSR1/2: user‑defined. Behavior is app‑specific.

Exit Codes

  • 0: success.
  • 1: generic failure.
  • >1: app‑defined or OS‑defined errors. Keep them documented.

Examples (English)

// Read argv & env
console.log(process.argv);
console.log("PORT:", process.env.PORT);

// Change CWD safely
try {
  process.chdir("/tmp");
  console.log("Now in:", process.cwd());
} catch (err) {
  console.error("chdir failed:", err.message);
}

// Graceful shutdown on SIGINT (Ctrl+C)
process.on("SIGINT", () => {
  console.log("Shutting down...");
  process.exit(0);
});

// Schedule: nextTick vs setImmediate
process.nextTick(() => console.log("microtask"));
setImmediate(() => console.log("macrotask"));

// Set exit code without immediate exit
process.exitCode = 1;

Notes

  • Prefer setting process.exitCode over calling process.exit() directly in libraries; let the event loop flush.
  • Do not mutate process.env for cross‑module config; read once and pass explicit options.
  • Always handle signals to close files, servers, and timers gracefully.
  • Use nextTick for microtask‑like callbacks; use setImmediate to defer after I/O.