Skip to content

process & env

process is a global object that gives your program access to the running Node.js process. It is available everywhere — no import required.

The most commonly used properties are:

Property / MethodWhat it gives you
process.argvArray of command-line arguments
process.envObject of environment variables
process.cwd()Current working directory string
process.exit(code)Terminate the process with an exit code
process.platformOS platform string ('linux', 'darwin', 'win32')
process.versionNode.js version string (e.g. 'v20.11.0')

process.argv is always an array. The first two elements are fixed:

  • argv[0] — path to the node binary
  • argv[1] — path to the script being run
  • argv[2] onwards — your actual arguments
Node.js

Needs the Node.js runtime — open in StackBlitz to run.

process.env is a plain object whose keys and values are strings. It is populated from the shell environment at process startup.

Node.js

Needs the Node.js runtime — open in StackBlitz to run.

Node.js

Needs the Node.js runtime — open in StackBlitz to run.

process.exit(code) terminates the process immediately. By convention:

  • Exit code 0 means success.
  • Any non-zero code (typically 1) means failure.
// Example: exit with error if a required env var is missing
if (!process.env.DATABASE_URL) {
console.error('ERROR: DATABASE_URL is required');
process.exit(1);
}

Calling process.exit() is a hard stop — finally blocks still run, but pending async callbacks do not. For a graceful shutdown, prefer draining the server first (e.g., server.close()), then letting the event loop exit naturally.

What does process.argv[0] contain when you run `node script.js`?
What type are the values stored in process.env?
Which exit code conventionally indicates successful completion?