The `unload` package provides a unified API for executing code reliably when a JavaScript process or environment is about to exit or unload. It abstracts away environment-specific mechanisms like `process.on('beforeExit')`, `process.on('SIGINT')`, `process.on('uncaughtException')` in Node.js, and `window.addEventListener('beforeunload')` or `window.addEventListener('unload')` in browsers, Electron, React Native, workers, and iframes. This ensures the registered exit functions are called only once, regardless of how the environment terminates. The current stable version is 2.4.1. It is particularly useful for library authors who need to clean up resources consistently across diverse JavaScript runtimes, ensuring robust shutdown procedures. It differs from simply using `process.on('exit')` by also handling abnormal terminations like `SIGINT` and `uncaughtException` in Node.js, and browser tab/window closures.
npm install unloadVerified import paths — ran on the pinned version, not inferred.
Registers a cleanup function to run on process exit, demonstrates adding and removing handlers, and shows how it's used with TypeScript.
Ensure cleanup logic is as fast as possible. For critical async tasks, consider handling them earlier in the application lifecycle or logging potential failures for post-mortem analysis.
Design handlers to be idempotent and self-contained. Avoid inter-dependencies between different `unload.add()` calls.
Migrate CommonJS `require('unload')` statements to ESM `import unload from 'unload';` for better tree-shaking and future compatibility in modern environments.Ensure `unload` is imported as a default export: `import unload from 'unload';` for ESM, or `const unload = require('unload');` for CommonJS. Do not try `import { add } from 'unload';` as `add` is not a named export directly.Add `import unload from 'unload';` at the top of your ESM file or `const unload = require('unload');` for CommonJS files where `unload` is used.Run `npm install unload` or `yarn add unload` to install the package. Verify that your build configuration (webpack, Rollup, etc.) correctly resolves `node_modules`.
No dependency data recorded yet.