exit-hook vs node-cleanup vs death
Node.js Process Termination Libraries Comparison
1 Year
exit-hooknode-cleanupdeathSimilar Packages:
What's Node.js Process Termination Libraries?

These libraries provide mechanisms to handle process termination in Node.js applications, allowing developers to execute cleanup tasks or perform specific actions when the process is about to exit. They help ensure that resources are released properly, and any necessary finalization logic is executed, which is crucial for maintaining application stability and preventing data loss.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
exit-hook2,736,00328211.3 kB5a year agoMIT
node-cleanup1,349,387168-98 years agoMIT
death206,540183-38 years ago-
Feature Comparison: exit-hook vs node-cleanup vs death

Exit Handling

  • exit-hook:

    'exit-hook' allows you to register multiple exit hooks that will be executed in the order they were registered. This feature is beneficial for applications that need to perform several cleanup operations in a specific sequence before termination.

  • node-cleanup:

    'node-cleanup' offers a unified approach to handle both exit signals and uncaught exceptions, allowing you to define cleanup logic that runs in both scenarios. This ensures that your application can gracefully handle unexpected errors and still perform necessary cleanup.

  • death:

    The 'death' package provides a simple API to register a callback that will be executed when the Node.js process is about to exit. This is useful for performing quick cleanup tasks without much overhead.

Ease of Use

  • exit-hook:

    While 'exit-hook' is slightly more complex due to its support for multiple hooks, it still maintains a user-friendly API. Developers can easily register and manage exit hooks without much hassle, making it accessible for most use cases.

  • node-cleanup:

    'node-cleanup' provides a more comprehensive API, which may require a bit more understanding to utilize fully. However, its robust features make it worthwhile for applications that need advanced exit handling capabilities.

  • death:

    'death' is designed to be minimalistic and easy to use, requiring very little setup. You can quickly integrate it into your application with just a few lines of code, making it ideal for developers looking for a straightforward solution.

Flexibility

  • exit-hook:

    'exit-hook' offers flexibility by allowing multiple handlers to be registered, which can be beneficial for applications with diverse cleanup needs. This flexibility allows developers to tailor the exit process according to their requirements.

  • node-cleanup:

    'node-cleanup' provides flexibility in handling both exit signals and exceptions, allowing developers to define cleanup logic that can adapt to various scenarios, enhancing the robustness of error handling.

  • death:

    'death' is focused solely on exit handling, making it a good choice for developers who want a lightweight solution without additional features that may complicate the implementation.

Performance

  • exit-hook:

    'exit-hook' is efficient in managing multiple hooks, but developers should be mindful of the order and complexity of the registered hooks to avoid performance bottlenecks during exit.

  • node-cleanup:

    'node-cleanup' is designed to handle cleanup efficiently, but the performance may vary depending on the complexity of the cleanup logic defined by the developer. It is important to optimize the cleanup tasks to ensure quick termination.

  • death:

    Due to its minimalistic design, 'death' has a low performance overhead, making it suitable for applications where performance is critical and only basic exit handling is needed.

Error Handling

  • exit-hook:

    'exit-hook' does not inherently handle errors but allows developers to register hooks that can include error handling logic, providing some level of control over how errors are managed during the exit process.

  • node-cleanup:

    'node-cleanup' excels in error handling by allowing cleanup tasks to be executed during both exit signals and uncaught exceptions. This feature ensures that developers can manage resources effectively, even in the event of unexpected errors.

  • death:

    'death' does not provide built-in error handling capabilities, focusing solely on exit events. Developers need to implement their own error handling logic if required.

How to Choose: exit-hook vs node-cleanup vs death
  • exit-hook:

    Opt for 'exit-hook' if you require more control over the exit process and want to register multiple exit handlers that can execute in a specific order. It is useful for applications that need to perform various cleanup tasks before exiting, such as closing database connections or saving state.

  • node-cleanup:

    Select 'node-cleanup' if you need a robust solution that can handle both process termination and uncaught exceptions. It allows you to define cleanup logic that runs on process exit or when an error occurs, making it ideal for applications that require comprehensive error handling and resource management.

  • death:

    Choose 'death' if you need a simple and straightforward solution to handle process termination events, particularly if you want to execute a callback function when the process exits. It is lightweight and easy to integrate into existing applications.

README for exit-hook

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(signal => {
	console.log(`Exiting with signal: ${signal}`);
});

// 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: (signal: number) => void

The callback function to execute when the process exits.

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.

onExit

Type: (signal: number) => (void | Promise<void>)

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

options

Type: object

wait

Type: number

The amount of time in milliseconds that the onExit function is expected to take. When multiple async handlers are registered, the longest wait time will be used.

import {asyncExitHook} from 'exit-hook';

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

throw new Error('🦄');

//=> 'Exiting'

Removing an asynchronous exit hook:

import {asyncExitHook} from 'exit-hook';

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

unsubscribe();

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