{"library":"node-cleanup","title":"Node Process Cleanup Handler","description":"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.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install node-cleanup"],"cli":null},"imports":["const nodeCleanup = require('node-cleanup');","nodeCleanup.uninstall();"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"const nodeCleanup = require('node-cleanup');\nlet unsavedData = {\n    data: 'some critical data', // Example data to save\n    save: function(callback) {\n        console.log('Saving unsaved data:', this.data);\n        setTimeout(() => {\n            this.data = ''; // Simulate data being saved\n            callback();\n        }, 100); // Simulate async save operation\n    }\n};\n\nnodeCleanup(function (exitCode, signal) {\n    if (signal) {\n        console.log(`Received signal: ${signal}. Initiating async cleanup.`);\n        unsavedData.save(function done() {\n            // Important: calling process.exit() here would terminate immediately\n            // and not inform the parent process of the signal. Re-emit the signal.\n            process.kill(process.pid, signal);\n        });\n        nodeCleanup.uninstall(); // Prevent handler from being called again on re-emitted signal\n        return false; // Prevent immediate process exit to allow async work\n    }\n    console.log(`Exiting with code: ${exitCode}. Sync cleanup finished.`);\n}, {\n    ctrl_C: \"{^C} caught. Saving data...\", // Custom message for Ctrl+C\n    uncaughtException: \"Uh oh. An uncaught exception occurred:\"\n});\n\nconsole.log(\"Process running. Press Ctrl+C to test async cleanup.\");\n// Simulate some ongoing work to keep the process alive\nsetInterval(() => {\n    // This interval keeps the Node.js process from exiting naturally\n    // until a signal or an uncaught exception occurs.\n}, 1000);","lang":"javascript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}