CLI Unhandled Rejection Handler

1.1.2 · active · verified Wed Apr 22

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

Warnings

Install

Imports

Quickstart

Demonstrates how to initialize cli-handle-unhandled and triggers an unhandled promise rejection to show its intended behavior of crashing the process.

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);

view raw JSON →