Gathering detailed insights and metrics for exit-hook
Gathering detailed insights and metrics for exit-hook
Gathering detailed insights and metrics for exit-hook
Gathering detailed insights and metrics for exit-hook
async-exit-hook
Run some code when the process exits (supports async hooks and pm2 clustering)
when-exit
Execute a function right before the process, or the browser's tab, is about to exit.
@types/async-exit-hook
TypeScript definitions for async-exit-hook
@alwatr/exit-hook
A utility for registering exit handlers in Node.js.
npm install exit-hook
Typescript
Module System
Min. Node Version
Node Version
NPM Version
99.8
Supply Chain
99.5
Quality
75.8
Maintenance
100
Vulnerability
100
License
JavaScript (94.3%)
TypeScript (5.7%)
Total Downloads
1,045,601,813
Last Day
249,698
Last Week
3,535,083
Last Month
14,552,925
Last Year
132,558,125
MIT License
289 Stars
42 Commits
40 Forks
9 Watchers
1 Branches
9 Contributors
Updated on Jun 27, 2025
Minified
Minified + Gzipped
Latest Version
4.0.0
Package Id
exit-hook@4.0.0
Unpacked Size
11.04 kB
Size
3.92 kB
File Count
5
NPM Version
9.2.0
Node Version
18.16.1
Published on
Aug 31, 2023
Cumulative downloads
Total Downloads
Last Day
26.5%
249,698
Compared to previous day
Last Week
-0.7%
3,535,083
Compared to previous week
Last Month
9.2%
14,552,925
Compared to previous month
Last Year
28.5%
132,558,125
Compared to previous year
Run some code when the process exits
The process.on('exit')
event doesn't catch all the ways a process can exit.
This package is useful for cleaning up before exiting.
1npm install exit-hook
1import exitHook from 'exit-hook'; 2 3exitHook(signal => { 4 console.log(`Exiting with signal: ${signal}`); 5}); 6 7// You can add multiple hooks, even across files 8exitHook(() => { 9 console.log('Exiting 2'); 10}); 11 12throw new Error('🦄'); 13 14//=> 'Exiting' 15//=> 'Exiting 2'
Removing an exit hook:
1import exitHook from 'exit-hook'; 2 3const unsubscribe = exitHook(() => {}); 4 5unsubscribe();
Register a function to run during process.exit
.
Returns a function that removes the hook when called.
Type: (signal: number) => void
The callback function to execute when the process exits.
Register a function to run during gracefulExit
.
Returns a function that removes the hook when called.
Please see Async Notes for considerations when using the asynchronous API.
Type: (signal: number) => (void | Promise<void>)
The callback function to execute when the process exits via gracefulExit
, and will be wrapped in Promise.resolve
.
Type: object
Type: number
The amount of time in milliseconds that the onExit
function is expected to take. When multiple async handlers are registered, the longest wait
time will be used.
1import {asyncExitHook} from 'exit-hook'; 2 3asyncExitHook(async () => { 4 console.log('Exiting'); 5}, { 6 wait: 300 7}); 8 9throw new Error('🦄'); 10 11//=> 'Exiting'
Removing an asynchronous exit hook:
1import {asyncExitHook} from 'exit-hook'; 2 3const unsubscribe = asyncExitHook(async () => { 4 console.log('Exiting'); 5}, { 6 wait: 300 7}); 8 9unsubscribe();
Exit the process and make a best-effort to complete all asynchronous hooks.
If you are using asyncExitHook
, consider using gracefulExit()
instead of process.exit()
to ensure all asynchronous tasks are given an opportunity to run.
1import {gracefulExit} from 'exit-hook'; 2 3gracefulExit();
Type: number
Default: 0
The exit code to use. Same as the argument to process.exit()
.
tl;dr If you have 100% control over how your process terminates, then you can swap exitHook
and process.exit
for asyncExitHook
and gracefulExit
respectively. Otherwise, keep reading to understand important tradeoffs if you're using asyncExitHook
.
Node.js does not offer an asynchronous shutdown API by default #1 #2, so asyncExitHook
and gracefulExit
will make a "best effort" attempt to shut down the process and run your asynchronous tasks.
If you have asynchronous hooks registered and your Node.js process is terminated in a synchronous manner, a SYNCHRONOUS TERMINATION NOTICE
error will be logged to the console. To avoid this, ensure you're only exiting via gracefulExit
or that an upstream process manager is sending a SIGINT
or SIGTERM
signal to Node.js.
Asynchronous hooks should make a "best effort" to perform their tasks within the wait
time, but also be written to assume they may not complete their tasks before termination.
No vulnerabilities found.
Reason
security policy file detected
Details
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
0 existing vulnerabilities detected
Reason
Found 10/30 approved changesets -- score normalized to 3
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-06-23
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More