npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@scg82/exit-hook

v3.4.1

Published

Execute a callback when the process exits, passing the exit code (ESM/CJS)

Downloads

1,381

Readme

exit-hook

Execute a callback when the process exits, passing the exit code

This is a fork of sindresorhus/exit-hook with the following changes:

  • Passes the process exit code as the argument to the hook function
  • Accepts a number for the second argument to asyncExitHook to set the minimumWait option
  • ESM/CJS compatible

The process.on('exit') event doesn't catch all the ways a process can exit.

This package is useful for cleaning up before exiting (e.g. terminating subprocesses).

Install

npm install exit-hook

Usage

import exitHook from 'exit-hook';

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

// 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)

Register a function to run during process.exit.

Returns a function that removes the hook when called.

onExit

Type: function(exitCode: number): void

The callback function to execute when the process exits.

asyncExitHook(onExit, minimumWait)

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.

import {asyncExitHook} from 'exit-hook';

asyncExitHook(async (code) => {
	console.log('Exiting with code', code);
});

throw new Error('🦄');

//=> 'Exiting'

Removing an asynchronous exit hook:

import {asyncExitHook} from 'exit-hook';

const unsubscribe = asyncExitHook(async () => {
	console.log('Exiting');
}, {
	minimumWait: 300
});

unsubscribe();

onExit

Type: function(exitCode: number): void | Promise<void>

The callback function to execute when the process exits via gracefulExit, and will be wrapped in Promise.resolve.

minimumWait

Type: number
Default: 500

The amount of time in milliseconds that the onExit function is expected to take.

options

minimumWait

Type: number
Default: 500

The amount of time in milliseconds that the onExit function is expected to take.

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.

import {gracefulExit} from 'exit-hook';

gracefulExit();

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 minimumWait time, but also be written to assume they may not complete their tasks before termination.