WeCodeAcademy

Node.js process and Environment

ExampleNode.js
const args = process.argv.slice(2);
console.log("arguments:", args.length);

process.env.GREETING = "Hello";
console.log(process.env.GREETING, process.env.MISSING_VAR);
console.log(process.env.MISSING_VAR || "default value");

console.log(typeof process.pid, process.platform.length > 0);
console.log(typeof process.cwd());

process.on("exit", (code) => console.log("exiting with", code));
console.log("last line of the program");
Output
arguments: 0
Hello undefined
default value
number true
string
last line of the program
exiting with 0

process.argv lists command line arguments, process.env holds environment variables (the usual place for secrets and settings), and process.on('exit') runs when the program ends.

Quick check

Where do environment variables live in Node?

Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.

Tip: use the left and right arrow keys to move between lessons.