Node.js theorytheory 0/50 · 0%
Foundations · easy

7. process and environment

Reading configuration and exiting cleanly.

The global `process` object exposes the running program: `process.argv` (CLI arguments), `process.env` (environment variables), `process.exit(code)`, and `process.on("SIGINT", ...)` for signal handling.

js
const rpcUrl = process.env.RPC_URL ?? "https://mainnet.base.org";
const [, , command] = process.argv; // argv[0]=node, argv[1]=script path

process.on("SIGINT", () => {
  console.log("shutting down gracefully");
  process.exit(0);
});

Configuration (API keys, RPC endpoints, database URLs) should live in environment variables, not hard-coded in source — this is how the same code runs against testnet and mainnet, and it keeps secrets out of git history.

Check your understanding

  1. 1. Where should an RPC endpoint or API key typically live?

  2. 2. What does `process.argv[0]` typically contain?

  3. 3. What does `process.exit(0)` signal?