process
| Exit codes | |
| SIGINT sketch | |
| Working directory | |
| process.version |
Exit codes
Zero means success and any non-zero value means failure. Scripts and CI systems branch on that number, so choose it with care.
const words: string[] = process.argv.slice(2);
if (words.length === 0) {
console.log("missing name");
process.exit(1);
}
console.log("ok");
node main.js
missing name
SIGINT sketch
Ctrl plus C sends SIGINT to ask a running tool to stop. A tiny listener saves state and exits cleanly instead of halting mid-write.
process.on("SIGINT", () => {
console.log("shutting down");
process.exit(0);
});
Keep the handler short: flush buffers, close files, then exit. Heavy work belongs in normal shutdown paths, not in a signal sketch.
Working directory
The working directory is where the command was launched, which may differ from the script folder. Log it when relative paths behave oddly.
const here: string = process.cwd();
console.log(here);
Resolve user-supplied relative paths against this value. Resolve app-owned assets against the script folder from the path lesson instead.
process.version
The version string names the exact runtime in use. Print it in bug reports and in startup banners for long-lived tools.
console.log(process.version);
console.log(process.platform);
v16.20.2
Pair the version with the platform name for full context. That pair often explains why code passes on one machine and fails on another.
Next: Callbacks to promises
Article author: Arthur Isaev