The process Object
The process object is a built-in global that connects your Node program to the operating system, exposing command-line arguments, environment variables, the working directory, and methods to read input and control how the program exits.
Learn The process Object in our free Node.js course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Node.js course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
By the end of this lesson you'll be able to read arguments with process.argv, pull configuration from process.env, find the working directory, detect the platform, write to stdout, and stop your program cleanly with the right exit code.
What You'll Learn in This Lesson
1️⃣ Command-Line Arguments: process.argv
process.argv is an array describing how your program was launched. The first two entries are fixed — the path to node and the path to your script — so your own arguments start at index 2.
The standard move is process.argv.slice(2) , which drops those two fixed entries and gives you a clean array of just the values the user typed.
2️⃣ Environment Variables: process.env
process.env exposes the environment variables as an object of strings. This is the standard place to keep configuration that changes between machines — ports, API keys, database URLs — so it lives outside your code.
A variable that wasn't set reads as undefined , so the common pattern is a fallback: .
3️⃣ Platform, Output & Exiting
process.platform returns the OS family ( 'linux' , 'darwin' , or 'win32' ), handy for cross-platform behaviour. process.cwd() returns the directory you ran the command from, which isn't always where the file lives.
process.stdout.write prints text with no trailing newline (unlike console.log ). And process.exit(code) stops the program immediately — 0 for success, non-zero for failure — so any code after it never runs.
Your turn. Fill in the two blanks marked ___ so the program reads a name argument with a sensible fallback, then run it.
No blanks — just a brief and an outline. This ties together reading arguments, converting a string to a number, and exiting with the right code. Write it yourself, run it, and check your output.
📋 Quick Reference — The process Object
Practice quiz
Do you have to import the process object before using it?
- Yes, require('process')
- No, it is a built-in global
- Yes, import it from 'node:os'
- Only in ESM files
Answer: No, it is a built-in global. process is a global object Node provides automatically; you never import it.
For 'node greet.js Alice 42', what is process.argv.length?
- 4
- 2
- 3
- 1
Answer: 4. argv[0] is node, argv[1] is the script path, then 'Alice' and '42' make four entries.
At what index do your own command-line arguments start in process.argv?
- 0
- 1
- 2
- 3
Answer: 2. Index 0 is node and index 1 is the script path, so user arguments begin at index 2.
What does process.argv.slice(2) give you?
- The node path
- The script path
- Just the user-supplied arguments
- The environment variables
Answer: Just the user-supplied arguments. slice(2) drops the two fixed entries, leaving a clean array of the user's arguments.
What type are the values stored in process.env?
- Numbers
- Booleans
- Objects
- Strings
Answer: Strings. Environment variables are always strings; convert with Number() if you need a number.
What does process.env.PORT evaluate to if PORT was never set?
- undefined
- 0
- An empty string
- null
Answer: undefined. An unset variable reads as undefined, which is why the || '3000' fallback pattern is common.
What does process.exit(0) signal to whatever launched the program?
- Failure
- Success
- A warning
- Restart
Answer: Success. By convention exit code 0 means success and any non-zero value means failure.
How does process.stdout.write differ from console.log?
- It adds a trailing newline
- It only works on Windows
- It does not add a trailing newline
- It returns a Promise
Answer: It does not add a trailing newline. stdout.write prints raw text with no automatic newline, unlike console.log.
What does process.platform return on Linux?
- 'win32'
- 'linux'
- 'darwin'
- 'Linux_NT'
Answer: 'linux'. process.platform returns the OS family code: 'linux', 'darwin', or 'win32'.
What happens to code written after a process.exit() call?
- It runs after a delay
- It runs first
- It never runs
- It runs only on error
Answer: It never runs. exit() stops the program immediately, so any code after it never executes.