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.
process.exitCode = 1;
Try it
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.
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
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 beatsexitCode.process.exit()without one usesexitCode. An uncaught exception sets the code to1no matter what you assigned. - The
'exit'listener receives the final code, and listeners may still changeprocess.exitCodethere. - 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 --testand most build tools finish withexitCode = 1when something fails, so the final report still prints. - A hanging program never exits.
exitCodedoes 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.