CLI Alerts
raw JSON →cli-alerts is a lightweight, cross-platform utility for Node.js command-line interfaces, designed to display formatted alerts with colored text and symbolic icons. It supports various alert types including success, info, warning, and error, ensuring consistent visual feedback across macOS, Linux, and Windows terminals. The current stable version is 2.0.0. While not following a strict time-based release cadence, the project maintains an active development status with periodic improvements and bug fixes. Its key differentiators include its simplicity, minimal dependencies, and direct focus on enhancing the user experience of CLI tools by providing clear, visually distinct messages without complex configurations, making it an excellent choice for developers looking to add immediate, actionable feedback to their scripts and applications.
Common errors
error Error [ERR_REQUIRE_ESM]: require() of ES Module ... not supported ↓
const alert = require('cli-alerts'); to import alert from 'cli-alerts';. If your project is CommonJS, ensure cli-alerts itself is not an ESM-only package that's incompatible, or use a tool like ts-node or esm package for better interoperability. error TypeError: alert is not a function ↓
cli-alerts, in ESM, use import alert from 'cli-alerts'; (default import). In CommonJS, use const alert = require('cli-alerts');. Warnings
breaking The package moved to v2.0.0, indicating potential breaking changes according to semantic versioning. While specific details were not provided in the readily available documentation, users upgrading from v1.x should review the changelog for any API alterations. ↓
gotcha When using ESM in a Node.js project, ensure your `package.json` specifies `"type": "module"` or use `.mjs` file extensions. Mixing CommonJS `require()` with ESM `import` in the same file can lead to `ERR_REQUIRE_ESM` errors. ↓
Install
npm install cli-alerts yarn add cli-alerts pnpm add cli-alerts Imports
- alert wrong
import { alert } from 'cli-alerts'correctimport alert from 'cli-alerts' - alert (CommonJS)
const alert = require('cli-alerts'); - AlertOptions (type)
import type { AlertOptions } from 'cli-alerts';
Quickstart
import alert from 'cli-alerts';
type AlertOptions = {
type: 'success' | 'info' | 'warning' | 'error';
msg: string;
name?: string;
};
const showAlert = (options: AlertOptions) => {
alert(options);
};
// Example usage:
console.log('\n--- CLI Alerts Examples ---');
showAlert({ type: 'success', msg: 'Operation completed successfully!' });
showAlert({ type: 'info', msg: 'Checking for updates...' });
showAlert({ type: 'warning', msg: 'Configuration file missing, using defaults.', name: 'CONFIG' });
showAlert({ type: 'error', msg: 'Failed to connect to the server. Please try again.' });
console.log('--- End Examples ---\n');
// To run this:
// 1. npm install cli-alerts
// 2. Save as 'app.ts'
// 3. npx ts-node app.ts