process

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

echo $?

1

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);

v24.22.3
linux

Pair the version with the platform name for full context. That pair often explains why code passes on one machine and fails on another.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does exit code 0 mean?

    Answer

    Success

    Shells and CI systems read zero as success and any non-zero value as failure.

  2. What does SIGINT ask a program to do?

    Answer

    Stop, usually after Ctrl plus C

    The interrupt signal is what the terminal sends on Ctrl plus C.

  3. How does process.cwd() differ from the script folder?

    Answer

    cwd is where the command was launched; the script folder is where the file lives

    A relative path resolves against the launch folder, which may differ from the file location.

  4. What does process.version contain?

    Answer

    The version of the running Node runtime

    The string names the exact runtime, as in v24.22.3.

  5. What does this program print? const words: string[] = process.argv.slice(2); if (words.length === 0) { console.log("missing name"); process.exit(1); } console.log("ok"); with no arguments?

    Answer

    missing name

    The empty word list takes the failure branch, prints the line, and exits with status 1.

  6. What does this program print? const words: string[] = ["Ada"]; if (words.length === 0) { console.log("missing name"); process.exit(1); } console.log("ok");

    Answer

    ok

    The list is not empty, so the success line prints and the exit is 0.

  7. What does this program print? console.log(typeof process.version);

    Answer

    string

    The version is text such as v24.22.3, so typeof reports string.

  8. What does this program print? console.log(process.platform.length > 0);

    Answer

    true

    The platform name is a non-empty string such as linux.

  9. What does the compiler say about this program? const code: string = process.exitCode; console.log(code);

    Answer

    error TS2322: Type 'string | number | null | undefined' is not assignable to type 'string'.

    The exit code type is wider than a string, so the assignment is refused.

  10. What does the compiler say about this program? const v: number = process.version; console.log(v);

    Answer

    error TS2322: Type 'string' is not assignable to type 'number'.

    The version is text, and a number variable cannot hold it.

Article author: Arthur Isaev