Skip to content
Tutorials Center
My learning

Topics

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.

NODE
process.on('exit', (code) => console.log('bye', code));
process.exit(3);

Try it

Node.js Exit runs the exit listeners only

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 Setting the code later and graceful stop

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.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, await continuations, nextTick callbacks, promise callbacks and 'beforeExit' do not run afterwards.
  • Output can be lost. Writes to pipes and files by process.stdout and process.stderr can be asynchronous. Calling process.exit() right after a large console.log may cut the text off when output goes to a pipe. Set process.exitCode instead, 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: 0 success, 1 general failure, 2 misuse, 130 stopped by Ctrl+C. Node itself uses 1 for uncaught exceptions and 7 when an uncaught-exception handler throws.
  • Signal deaths (for example SIGTERM) do not call process.exit(); the shell sees 128 + signal number. Handle signals explicitly if you need cleanup, see Signal events.
  • Inside an 'exit' listener, calling process.exit() again with a code replaces the pending code.
  • process.reallyExit() is internal. Do not use it.