🧠 The process Object
process is a global object in Node.js. It gives you details about the running Node.js process — environment info, arguments, memory usage, and more.
No import needed — it's available everywhere by default.
🔧 Common Uses
1️⃣ Environment Variables
System-level values — usernames, tokens, ports — live here:
console.log(process.env);
// all environment variables, e.g. { USER: 'admin', LANG: 'en_US.UTF-8', ... }
console.log(process.env.USERNAME); // e.g. JaneSmith
console.log(process.env.PATH); // e.g. /usr/local/bin:/usr/bin:/bin✅ Handy for secure keys, ports, or secrets in larger apps.
💡 In real projects, these usually come from a .env file loaded with a package like dotenv, rather than being set manually every time.
2️⃣ Exiting the Process
process.exit(); // stop the app immediately
process.exit(1); // stop with an error code✅ Useful when a script finishes early or hits an error and needs to stop.
⚠️ Use sparingly — exiting abruptly can cut off things like unflushed logs or pending I/O. Let the process end naturally when you can.
3️⃣ Process Info
console.log(process.pid); // Process ID, e.g. 4821
console.log(process.version); // Node version, e.g. v18.16.0
console.log(process.platform); // OS, e.g. 'win32' or 'darwin'
console.log(process.cwd()); // current working directory4️⃣ Memory Usage
console.log(process.memoryUsage());
// { rss: 25M, heapTotal: 10M, heapUsed: 6M, external: 1M, ... }✅ Good for spotting memory leaks or checking how heavy a script is.
🧾 Command-Line Arguments
When you run a script from the terminal, anything typed after the filename is an argument. These let a script take external input — filenames, modes, user values, and so on.
Node.js collects them all in one array:
process.argv;Holds everything typed in the terminal for that run.
The first two entries are always:
process.argv[0]→ path to the Node.js binaryprocess.argv[1]→ path to your script file
Your actual arguments start from index
2onward.
✨ Example
greet.js:
const args = process.argv;
console.log(args);Run it:
node greet.js codeveda 2025[
'C:\\Program Files\\nodejs\\node.exe',
'C:\\Users\\Varsha\\OneDrive\\greet.js',
'codeveda',
'2025'
]Grab the real arguments like this:
const name = process.argv[2];
const year = process.argv[3];
console.log(`Welcome to ${name}, you are here in ${year}`);💡 A common shortcut is process.argv.slice(2) — it drops the node path and file path, leaving just the values you actually care about:
const [name, year] = process.argv.slice(2);💡 Real-World Use Cases
📄 File input —
node app.js input.txtreads a file passed as an argument➕ Math operations —
node calc.js add 5 3does addition using CLI arguments🚩 Custom flags —
node app.js --env=prod --debugtoggles options dynamically📦 CLI tools — packages like
npmandgitare built entirely around parsingargvstyle input