Beginner Node.js Interview QuestionsBeginnerConcept
Node.js · Question 21
What is the process object in Node.js?
Direct answer
process is a global Node object representing the running Node process — it exposes env vars, argv, cwd, exit codes, stdin/stdout/stderr, and lifecycle events like beforeExit and signal handlers for graceful shutdown.
Every Node program has exactly one process instance. Servers read PORT from process.env; CLIs parse process.argv; production apps listen for SIGTERM to drain connections (Q73).
| Property / method | Purpose |
|---|---|
| process.env | Environment variables (strings) |
| process.argv | CLI args — [node, script, ...flags] |
| process.cwd() | Current working directory |
| process.exit(code) | Terminate process — 0 success, non-zero error |
| process.pid | OS process ID |
| process.on('SIGTERM') | Graceful shutdown hook in containers |
- stdin / stdout / stderr — streams for CLI tools and piping: node script.js | grep foo.
- process.version — Node semver string; pair with process.versions.v8 (Q12).
- uncaughtException — last-resort handler; prefer fixing errors, not relying on it (Q63).
process-basics.mjs
const port = Number(process.env.PORT) || 4000;
const isProd = process.env.NODE_ENV === "production";
console.log("Node", process.version, "pid", process.pid);
console.log("Args:", process.argv.slice(2));
process.on("SIGTERM", () => {
console.log("Shutting down gracefully…");
server.close(() => process.exit(0));
});