Node.js Reference ยท process
Node.js process.exit() method
Reference for process.exit() in Node.js: ending the process with a status code, what still runs on exit, why pending output can be lost, and safer alternatives.
process.exit() ends the Node.js process as soon as possible. It runs the synchronous 'exit' listeners, then stops, even if timers, sockets or promises are still waiting. The optional argument is the exit status: 0 means success, any other number signals failure to the shell, to CI and to supervisors.
Because it cuts everything short, process.exit() is a blunt tool. Most programs should simply finish their work and let the event loop empty, or set process.exitCode and return.
process.on('exit', (code) => console.log('bye', code)); process.exit(3);
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.exit([code])
| Name | Description |
|---|---|
code |
Optional integer or integer-like string. Defaults to process.exitCode if set, otherwise 0. |
Returns: it never returns.
Errors / edge cases: a code of another type (such as 'abc' or an object) throws ERR_INVALID_ARG_TYPE. Values outside 0 to 255 are wrapped by the operating system on most platforms, so use small numbers. In a worker thread, process.exit() ends only that worker, not the whole program.
Usage notes
- What runs: the
'exit'event listeners run synchronously, in the order registered. Timers, I/O callbacks,awaitcontinuations,nextTickcallbacks, promise callbacks and'beforeExit'do not run afterwards. - Output can be lost. Writes to pipes and files by
process.stdoutandprocess.stderrcan be asynchronous. Callingprocess.exit()right after a largeconsole.logmay cut the text off when output goes to a pipe. Setprocess.exitCodeinstead, or end the stream and wait for it to finish. - Prefer a natural exit. Closing servers, clearing intervals and awaiting your work leaves the loop empty, and Node exits on its own with the code in
process.exitCode. - Do not call it in libraries. A package that exits the process takes control away from the application. Throw an error and let the top-level code decide.
- Conventional codes:
0success,1general failure,2misuse,130stopped by Ctrl+C. Node itself uses1for uncaught exceptions and7when an uncaught-exception handler throws. - Signal deaths (for example
SIGTERM) do not callprocess.exit(); the shell sees128 + signal number. Handle signals explicitly if you need cleanup, see Signal events. - Inside an
'exit'listener, callingprocess.exit()again with a code replaces the pending code. process.reallyExit()is internal. Do not use it.