Node.js Reference ยท process
The Node.js exit and beforeExit events
Reference for the process exit and beforeExit events in Node.js: when each fires, what you may do inside, and how to run cleanup safely when the event loop empties.
Two events on process tell you the program is ending. 'beforeExit' fires when the event loop has run out of work but the process has not yet stopped; you may schedule new work from it and keep the program alive. 'exit' fires when the process is really about to stop, and its listeners can only run synchronous code.
Together they are the place for last-minute bookkeeping: flushing a counter, printing a summary, or restarting a job loop.
process.on('beforeExit', (code) => console.log('loop empty', code)); process.on('exit', (code) => console.log('exiting', code));
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.on('beforeExit', (code) => { /* may be async */ }) process.on('exit', (code) => { /* synchronous only */ })
| Name | Description |
|---|---|
code |
The exit code that will be used: process.exitCode or 0. |
Returns: process.on() returns process for chaining. The listeners' return values are ignored.
Errors / edge cases: a listener that throws in 'exit' ends the process with a failure code. Asynchronous work started in an 'exit' listener is abandoned.
Usage notes
beforeExitis not emitted when the process ends because ofprocess.exit()or an uncaught exception. Only a naturally emptied loop triggers it.beforeExitcan fire more than once. Every time the loop drains again after you scheduled work, it fires again. Guard against an endless cycle with a flag or counter, as in the second example.exitfires exactly once and runs in every normal ending: natural drain,process.exit(), and an uncaught exception (after the error is printed). It does not run when the process is killed by a signal you did not handle, or afterprocess.abort().- Only synchronous code works in
exit. Timers, promises and I/O callbacks queued there never run. Use synchronous file functions such asfs.writeFileSync()for a last save. - Do not use either event to wait for async cleanup. Do cleanup before you stop: close the server, await pending requests, then let the program leave. See Signal events.
- Changing the exit code: assign
process.exitCodeinside'exit'to override the status. - Worker threads have their own
'exit'and'beforeExit'events on theirprocessobject. - Listener leaks: each
process.oncall adds a listener. Warnings appear after more than 10 for the same event name.