CLI Unhandled Rejection Handler
cli-handle-unhandled is a specialized Node.js utility package designed for command-line interfaces (CLIs) to ensure graceful, yet explicit, termination upon encountering unhandled promise rejections. Currently stable at version 1.1.2, the package has a mature and focused scope, with infrequent but consistent maintenance releases since its initial launch. Its key differentiator lies in its opinionated approach: rather than merely logging an unhandled rejection and allowing the process to continue in an indeterminate state, it registers a handler that explicitly crashes the Node.js process, providing immediate feedback essential for robust CLI tooling. This prevents silent failures that can occur when asynchronous operations reject without a `.catch()` block, making errors transparent to the user or calling environment. It is primarily a CommonJS module, targeting Node.js environments.
Common errors
-
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module
cause Attempting to `require()` an ES Module or using `import` for this CommonJS package in a project configured for ESM.fixUse `const cliHandleUnhandled = require('cli-handle-unhandled');` in CommonJS files, or configure your build system to properly handle CommonJS modules within an ES Module environment. -
(node:XXXX) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch().
cause This is typically the warning that `cli-handle-unhandled` is designed to intercept. If you see this warning without a subsequent process exit, it means the handler might not be properly installed or another global unhandled rejection handler is overriding it.fixEnsure `cliHandleUnhandled()` is called early in your application's lifecycle. Check for other global `process.on('unhandledRejection', ...)` handlers that might be installed after `cli-handle-unhandled` and are preventing the intended crash.
Warnings
- gotcha The primary function of `cli-handle-unhandled` is to cause the Node.js process to exit (crash) gracefully when an unhandled promise rejection occurs. It does not 'prevent' crashes but rather ensures that unhandled rejections lead to an explicit, observable failure state, which is often desired in CLI tools to avoid silent errors.
- gotcha As a CommonJS module, `cli-handle-unhandled` must be imported using `require()`. Attempting to use ES Module `import` syntax in a pure ESM project without proper transpilation or wrapper might result in `ERR_REQUIRE_ESM`.
Install
-
npm install cli-handle-unhandled -
yarn add cli-handle-unhandled -
pnpm add cli-handle-unhandled
Imports
- unhandledError
import unhandledError from 'cli-handle-unhandled';
const unhandledError = require('cli-handle-unhandled'); - cliHandleUnhandled
import { cliHandleUnhandled } from 'cli-handle-unhandled';const cliHandleUnhandled = require('cli-handle-unhandled'); cliHandleUnhandled();
Quickstart
const cliHandleUnhandled = require('cli-handle-unhandled');
// Initialize the unhandled rejection handler
cliHandleUnhandled();
console.log('Application started. An unhandled promise rejection will occur in 1 second.');
async function simulateUnhandledRejection() {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log('Simulating an unhandled rejection...');
reject(new Error('This is an intentional unhandled rejection!')); // This will trigger cli-handle-unhandled
}, 1000);
});
}
// Call an async function without a .catch() to trigger an unhandled rejection
simulateUnhandledRejection();
// Keep the process alive briefly to allow the promise to reject
setTimeout(() => {
console.log('This message should not appear if cli-handle-unhandled works.');
}, 2000);