05 - Process & Environment
📋 Jump to TakeawaysThe process Object
process is a global. No import needed. It gives you info about the running Node.js process.
console.log(process.version); // v20.11.0
console.log(process.platform); // "darwin", "linux", or "win32"
console.log(process.pid); // 12345
console.log(process.cwd()); // /Users/you/projectEnvironment Variables
Access them through process.env. They're all strings.
console.log(process.env.HOME); // /Users/you
console.log(process.env.PATH); // /usr/local/bin:/usr/bin:...Set them when running a command:
PORT=3000 node server.jsconst port = process.env.PORT || 3000;
console.log(port); // "3000" (it's a string!)Remember: process.env values are always strings. process.env.PORT is "3000", not 3000.
.env Files
Node.js 20.6+ has built-in .env support:
# .env
DATABASE_URL=postgresql://localhost:5432/mydb
API_KEY=abc123node --env-file=.env app.jsconsole.log(process.env.DATABASE_URL);
// "postgresql://localhost:5432/mydb"No extra packages needed. For older Node versions, people use the dotenv package.
Command Line Arguments
process.argv is an array of everything passed to the command.
node app.js hello worldconsole.log(process.argv);
// [
// '/usr/local/bin/node', // [0] path to node
// '/Users/you/app.js', // [1] path to your script
// 'hello', // [2] first argument
// 'world' // [3] second argument
// ]
const args = process.argv.slice(2);
console.log(args); // ['hello', 'world']The first two elements are always node path and script path. Your actual arguments start at index 2.
Exit Codes
// Success (default)
process.exit(0);
// Error
process.exit(1);Exit code 0 means success. Anything else means failure. Other tools (CI, scripts) check this to know if your program worked.
if (!process.env.DATABASE_URL) {
console.error('DATABASE_URL is required');
process.exit(1);
}Standard I/O
Node.js has three standard streams:
// Write to stdout (normal output)
process.stdout.write('Hello\n');
// Write to stderr (errors, not captured by pipes)
process.stderr.write('Something went wrong\n');
// console.log uses stdout, console.error uses stderr
console.log('info'); // goes to stdout
console.error('error'); // goes to stderrThis matters when piping output:
node app.js > output.txt # stdout goes to file, stderr still shows
node app.js 2> errors.txt # stderr goes to fileHandling Signals
In a real app, you'd add these to a server or long-running process. Here's a quick example you can test:
process.on('SIGINT', () => {
console.log('Caught Ctrl+C, cleaning up...');
process.exit(0);
});
process.on('SIGTERM', () => {
console.log('Received kill signal, shutting down...');
process.exit(0);
});
// Keep the process alive so you can test Ctrl+C
console.log('Running... press Ctrl+C to stop');
let seconds = 0;
setInterval(() => {
seconds++;
console.log(`Running for ${seconds}s...`);
}, 1000);SIGINT is Ctrl+C. SIGTERM is what Docker and Kubernetes send to stop your process. Handle them for graceful shutdown.
Key Takeaways
processis global, gives you version, platform, pid, and cwdprocess.envvalues are always strings, convert numbers yourself- Node 20.6+ supports
--env-file=.envnatively, no dotenv needed process.argv.slice(2)gives you the actual command line arguments- Exit code 0 = success, anything else = failure
stdoutis for output,stderris for errors, they can be piped separately- Handle
SIGINTandSIGTERMfor graceful shutdown in servers