exit-hook

Run some code when the process exits

Usage no npm install needed!

<script type="module">
  import exitHook from 'https://cdn.skypack.dev/exit-hook';
</script>

README

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

$ npm install exit-hook

Usage

import exitHook from '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:

import exitHook from 'exit-hook';

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

unsubscribe();

API

exitHook(onExit)

Returns a function that removes the hook when called.

onExit

Type: Function

The callback function 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.