process & env
The process object
Section titled “The process object”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 / Method | What it gives you |
|---|---|
process.argv | Array of command-line arguments |
process.env | Object of environment variables |
process.cwd() | Current working directory string |
process.exit(code) | Terminate the process with an exit code |
process.platform | OS platform string ('linux', 'darwin', 'win32') |
process.version | Node.js version string (e.g. 'v20.11.0') |
process.argv — command-line arguments
Section titled “process.argv — command-line arguments”process.argv is always an array. The first two elements are fixed:
argv[0]— path to thenodebinaryargv[1]— path to the script being runargv[2]onwards — your actual arguments
Needs the Node.js runtime — open in StackBlitz to run.
process.env — environment variables
Section titled “process.env — environment variables”process.env is a plain object whose keys and values are strings. It is populated from the shell environment at process startup.
Needs the Node.js runtime — open in StackBlitz to run.
process.cwd() and process.platform
Section titled “process.cwd() and process.platform”Needs the Node.js runtime — open in StackBlitz to run.
process.exit()
Section titled “process.exit()”process.exit(code) terminates the process immediately. By convention:
- Exit code
0means success. - Any non-zero code (typically
1) means failure.
// Example: exit with error if a required env var is missingif (!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.