The `node-cleanup` package provides a mechanism to install custom cleanup handlers that execute when a Node.js process exits. This includes normal termination (exit code 0), error-induced exits (uncaught exceptions, exit code 1), and receipt of POSIX signals such as SIGINT (Ctrl-C), SIGHUP, SIGQUIT, and SIGTERM. The current stable version is 2.1.2, released recently, following a "complete rewrite" in version 2.1.0. Its release cadence appears to be driven by feature enhancements and major architectural changes rather than strict time intervals. Key differentiators include support for multiple independent handlers, asynchronous cleanup for signals (allowing postponement of process termination), delegation of termination decisions to child processes (useful for scenarios with tools like Emacs), and custom `stderr` messages for Ctrl-C and uncaught exceptions. The library focuses on robust and reliable process shutdown management in various scenarios.
npm install node-cleanupVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to install an asynchronous cleanup handler for POSIX signals like SIGINT (Ctrl-C), allowing critical data to be saved before the process fully terminates. It also configures custom `stderr` messages for signals and uncaught exceptions.
To enable default `stderr` messages, either call `nodeCleanup()` with no arguments (`nodeCleanup();`) or explicitly pass an `stderrMessages` object, even if empty, when combining with a handler (e.g., `nodeCleanup(myHandler, {});`). To customize messages, provide them in the `stderrMessages` object (e.g., `nodeCleanup({ ctrl_C: 'My custom Ctrl-C message.' })`).Ensure your signal cleanup handler explicitly `return false;` if you initiate asynchronous operations. Once cleanup is complete, re-emit the signal using `process.kill(process.pid, signal);` to properly terminate the process and inform the parent, rather than `process.exit()`.
Implement logic within the parent's cleanup handler to check for active child processes (e.g., `if (child !== null && signal === 'SIGINT') return false;`) and only allow the parent to exit after the child has handled the signal or exited completely.
Modify your cleanup handler to include `return false;` when handling a signal and initiating async work. Once the async work is done, explicitly re-emit the signal using `process.kill(process.pid, signal);` to ensure proper termination.
For `node-cleanup`, use `const nodeCleanup = require('node-cleanup');` in CommonJS modules. If you are in an ES Module context, consider using a dynamic import `import('node-cleanup')` or ensure your build system properly handles CommonJS interop.Explicitly configure `stderrMessages` when calling `nodeCleanup()`. For example, `nodeCleanup(myHandler, { ctrl_C: 'Custom Ctrl-C message' });` to set custom messages with a handler, or `nodeCleanup({ ctrl_C: 'Custom Ctrl-C message' });` if only messages are needed, or simply `nodeCleanup();` to get the default messages without installing a cleanup handler.No dependency data recorded yet.