Skip to content
Run some code when the process exits
JavaScript TypeScript
Branch: master
Clone or download
sindresorhus Remove mention of `async-exit-hook`
It's not actively maintained anymore and it has some glaring bugs.
Latest commit b504031 Sep 13, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github Tidelift tasks May 31, 2019
.editorconfig Require Node.js 6 Mar 24, 2018
.gitattributes Add TypeScript definition (#12) Apr 16, 2019
.gitignore Require Node.js 6 Mar 24, 2018
.npmrc Require Node.js 6 Mar 24, 2018
.travis.yml Add TypeScript definition (#12) Apr 16, 2019
fixture.js
index.d.ts Add TypeScript definition (#12) Apr 16, 2019
index.js Return an unsubscribe function (#11) Apr 9, 2019
index.test-d.ts Add TypeScript definition (#12) Apr 16, 2019
license Require Node.js 6 Mar 24, 2018
package.json 2.2.0 Apr 16, 2019
readme.md Remove mention of `async-exit-hook` Sep 13, 2019
test.js Require Node.js 6 Mar 24, 2018

readme.md

exit-hook Build Status

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

$ npm install exit-hook

Usage

const exitHook = require('exit-hook');

exitHook(() => {
	console.log('Exiting');
});

// You can add multiple hooks, even across files
exitHook(() => {
	console.log('Exiting 2');
});

throw new Error('🦄');

//=> 'Exiting'
//=> 'Exiting 2'

Removing an exit hook:

const exitHook = require('exit-hook');

const unsubscribe = exitHook(() => {});

unsubscribe();

API

exitHook(callback)

Returns a function that removes the hook when called.

callback

Type: Function

The callback to execute when the process exits.


Get professional support for this package with a Tidelift subscription
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.
You can’t perform that action at this time.