Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including node-graceful-shutdown with all npm packages installed. Try it out:

var nodeGracefulShutdown = require("node-graceful-shutdown")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

node-graceful-shutdown v1.0.3

Gracefully shutdown your modular NodeJS application


npm License Build Status

Gracefully handle your modular NodeJS application's shutdown (termination), using dependencies.


It doesn't matter in which order and where you define these graceful handlers, node-graceful-shutdown will handle them appropriately and exit the process once all of them are processed.

import { onShutdown } from "node-graceful-shutdown";

// module1.js
onShutdown("http-server", async function () {
    // Stop your http server here.

// module2.js
onShutdown("message-bus", ["http-server"], async function () {
    // Close your RabbitMQ connection here ONLY AFTER http server's onShutdown completed.

// moduleX.js
onShutdown("database", ["http-server", "message-bus"], async function () {
    // Shut down your database here, ONLY AFTER http-server and message-bus are completed.

// After all handlers are processed without errors, process exits with code 0.
// Otherwise it exits with exit code 42759, or exit code 42758 if there are any errors in assigned shutdown handlers.

// If some of specified dependencies are not defined (like when "http-server" is missing in the above example),
// node-graceful-shutdown will run the current handler without waiting for the undefined dependency.

Features and Guidelines

This library, along existing ones, allow your application to be modular. You define a cleanup callback in-place, in the same module, where initialization happens. In addition, it allows specifying the order


  1. Please, do not use this module in libraries (modules, packages). Use for the end application only.
  2. Once imported, onShutdown is application-wide (in terms of a single process), so the callbacks and their dependencies will see each other when imported from multiple files.
  3. Circular shutdown handler dependencies will throw an error immediately once declared.
  4. There's also an onShutdownError export which takes an error as an argument when any of assigned shutdown handlers throw an error (added for very-very prudent programmers only).
  5. Importing this module deletes existing handlers (SIGINT, SIGTERM, SIGQUIT) if there are any (normally, there should be no other handlers).
  6. You may also consider defining constants


MIT © Nikita Savchenko

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free