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 🙏

© 2026 – Pkg Stats / Ryan Hefner

on-shutdown

v0.0.3

Published

A lightweight, zero-dependency Node.js utility for registering graceful shutdown handlers.

Downloads

281

Readme

on-shutdown

A robust, zero-dependency Node.js utility for handling graceful shutdowns and process cleanup.

It ensures your cleanup functions are reliably called when the process exits, whether triggered by signals (SIGINT, SIGTERM), errors (uncaught exceptions), or manual calls to process.exit().

Features

  • Simple API: A single on_shutdown function to register your cleanup logic.
  • Process Integration: Overrides process.exit to ensure cleanup hooks run even when you exit manually.
  • Error Safety: Automatically catches uncaughtException and unhandledRejection to log errors and shut down gracefully.
  • Ordered Execution: Functions are executed in a Last-In, First-Out (LIFO) order.
  • Async Support: Supports asynchronous cleanup functions (Promises/async-await).
  • Resilient: Robust error handling during the shutdown phase itself prevents hanging processes.
  • Customizable: Full control over which signals or events trigger shutdown and their exit codes.

Installation

npm install on-shutdown

Usage

Import the on_shutdown function and register the functions you want to run on exit.

// your-app.js
import { on_shutdown } from 'on-shutdown';
import process from 'node:process';

// --- Mock async functions ---
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));

// Register cleanup logic
on_shutdown(async () => {
    console.log('Closing database connection...');
    await sleep(100);
    console.log('Database connection closed.');
});

on_shutdown(() => {
    console.log('Sync cleanup task...');
});

// --- Initialization ---
console.log('Application running. Press Ctrl+C or wait for exit.');

// Even if you call process.exit(), hooks will run!
setTimeout(() => {
    console.log('Calling process.exit(0)...');
    process.exit(0);
}, 2000);

Output

Application running. Press Ctrl+C or wait for exit.
Calling process.exit(0)...
Sync cleanup task...
Closing database connection...
Database connection closed.

Default Behavior

By default, on-shutdown automatically sets up listeners for the following events:

| Event | Exit Code | Action | | :--- | :--- | :--- | | SIGINT | 130 | Standard shutdown (Ctrl+C). | | SIGTERM | 143 | Standard shutdown (Docker/Kubernetes). | | SIGHUP | 0 | Reload signal (treated as shutdown). | | error | 1 | Logs error via console.error and exits. | | uncaughtException | 1 | Logs error via console.error and exits. | | unhandledRejection| 1 | Logs error via console.error and exits. | | beforeExit | N/A | Runs hooks if event loop empties. |

API

on_shutdown(func)

Registers a function to be executed on process shutdown.

  • func (Function): The function to execute. Can be synchronous or asynchronous.
  • Execution Order: LIFO (Last-In, First-Out).

on_shutdown_error(func)

Define a custom error handler for errors that occur during the execution of shutdown hooks.

  • func (Function): A function that receives the error. Defaults to console.error.
import { on_shutdown_error } from 'on-shutdown';

on_shutdown_error(async (err) => {
    // Send to logging service instead of console
    await myLogger.error('Error during shutdown:', err);
});

shutdown(code)

Manually trigger the graceful shutdown sequence. This is the function that now powers process.exit().

  • code (Number): The exit code (optional).
import { shutdown } from 'on-shutdown';

// Trigger shutdown manually with exit code 1
await shutdown(1);

set_shutdown_listener(event, code, event_fn)

Register or modify a listener for a specific process event.

  • event (String): The process event name (e.g., 'SIGINT', 'my-custom-event').
  • code (Number): The exit code to use when this event triggers.
  • event_fn (Function, optional): A specific callback to run for this event before shutdown logic begins.
import { set_shutdown_listener } from 'on-shutdown';

// Change SIGINT to exit with code 0 instead of 130
set_shutdown_listener('SIGINT', 0);

// Listen to a custom signal
set_shutdown_listener('SIGUSR2', 0, () => console.log('Received SIGUSR2'));

unset_shutdown_listener(...events)

Removes default or custom listeners. Useful if you want to handle specific signals entirely on your own.

  • ...events (String[]): List of event names to remove.
import { unset_shutdown_listener } from 'on-shutdown';

// Stop handling unhandledRejection automatically
unset_shutdown_listener('unhandledRejection');