Installations
npm install exit-hook
Developer Guide
Typescript
Yes
Module System
ESM
Min. Node Version
>=18
Node Version
18.16.1
NPM Version
9.2.0
Score
99.8
Supply Chain
99.5
Quality
75.8
Maintenance
100
Vulnerability
100
License
Releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (94.3%)
TypeScript (5.7%)
Love this project? Help keep it running — sponsor us today! 🚀
Developer
sindresorhus
Download Statistics
Total Downloads
994,781,981
Last Day
499,930
Last Week
499,930
Last Month
8,000,356
Last Year
116,111,798
GitHub Statistics
283 Stars
42 Commits
39 Forks
10 Watching
1 Branches
9 Contributors
Bundle Size
1.79 kB
Minified
916.00 B
Minified + Gzipped
Sponsor this package
Package Meta Information
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
Total Downloads
Cumulative downloads
Total Downloads
994,781,981
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
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
exit-hook
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.
Install
1npm install exit-hook
Usage
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();
API
exitHook(onExit)
Register a function to run during process.exit
.
Returns a function that removes the hook when called.
onExit
Type: (signal: number) => void
The callback function to execute when the process exits.
asyncExitHook(onExit, options)
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.
onExit
Type: (signal: number) => (void | Promise<void>)
The callback function to execute when the process exits via gracefulExit
, and will be wrapped in Promise.resolve
.
options
Type: object
wait
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();
gracefulExit(signal?: number): void
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();
signal
Type: number
Default: 0
The exit code to use. Same as the argument to process.exit()
.
Asynchronous Exit Notes
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.
![Empty State](/_next/static/media/empty.e5fae2e5.png)
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
security policy file detected
Details
- Info: security policy file detected: .github/security.md:1
- Info: Found linked content: .github/security.md:1
- Info: Found disclosure, vulnerability, and/or timelines in security policy: .github/security.md:1
- Info: Found text in security policy: .github/security.md:1
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: license:0
- Info: FSF or OSI recognized license: MIT License: license:0
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
- Warn: no topLevel permission defined: .github/workflows/main.yml:1
- Info: no jobLevel write permissions found
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:16: update your workflow using https://app.stepsecurity.io/secureworkflow/sindresorhus/exit-hook/main.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:17: update your workflow using https://app.stepsecurity.io/secureworkflow/sindresorhus/exit-hook/main.yml/main?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/main.yml:21
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 npmCommand dependencies pinned
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'main'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 10 are checked with a SAST tool
Score
4.3
/10
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 MoreOther packages similar to 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.