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%)
Love this project? Help keep it running — sponsor us today! 🚀
Total Downloads
994,781,981
Last Day
499,930
Last Week
499,930
Last Month
8,000,356
Last Year
116,111,798
283 Stars
42 Commits
39 Forks
10 Watching
1 Branches
9 Contributors
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
Publised On
31 Aug 2023
Cumulative downloads
Total Downloads
Last day
0%
499,930
Compared to previous day
Last week
-68.9%
499,930
Compared to previous week
Last month
-5.6%
8,000,356
Compared to previous month
Last year
14.8%
116,111,798
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
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
security policy file detected
Details
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 10/30 approved changesets -- score normalized to 3
Reason
0 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
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-01-27
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