Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท process

Node.js process.exitCode property

Reference for process.exitCode in Node.js: setting the exit status without cutting off pending work, validation rules, and how it interacts with process.exit().

process.exitCode holds the status code Node.js will use when the process ends by itself. Assigning to it does not stop anything: timers, sockets and promises keep running, and when the event loop finally empties, the process exits with that number.

This is the recommended way to report failure from a script. You record the result and let the program shut down cleanly, instead of calling process.exit() and risking lost output.

NODE
process.exitCode = 1;

Try it

Node.js Recording a failure and finishing the work

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Node.js Validation and precedence

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Syntax

NODE
process.exitCode            // integer | undefined
process.exitCode = code
Name Description
code An integer, an integer-like string ('3'), or undefined / null to clear it.

Returns: reading gives the last value assigned (a number) or undefined.

Errors / edge cases: assigning 'abc' or an object throws ERR_INVALID_ARG_TYPE immediately, and a non-integer number such as 1.5 throws ERR_OUT_OF_RANGE. When nothing has set the code, the process exits with 0.

Usage notes

  • Precedence: process.exit(n) with an argument beats exitCode. process.exit() without one uses exitCode. An uncaught exception sets the code to 1 no matter what you assigned.
  • The 'exit' listener receives the final code, and listeners may still change process.exitCode there.
  • Set it as early as you know there is a failure and keep going. Several parts of a program can set it, and the last assignment wins. If you need "worst code wins", compare before assigning: process.exitCode = Math.max(process.exitCode ?? 0, 2).
  • Test runners and CLIs use it: node --test and most build tools finish with exitCode = 1 when something fails, so the final report still prints.
  • A hanging program never exits. exitCode does nothing if an open server or interval keeps the loop alive; close those handles.
  • Stay in range. Use 0 to 255. Larger numbers wrap on Linux and macOS (256 becomes 0), which could turn a failure into apparent success.
  • Worker threads: each worker has its own exitCode; the main thread reads it from the worker's 'exit' event.