CLI Utilities Toolkit
cli-util is an abandoned Node.js package providing a collection of utility functions designed for building command-line interface applications. It offers basic functionalities for logging, styled output (colors, headers, indentation), string padding, and column formatting. The package is built on top of very old versions of `colors` (0.6.2) and `commander` (2.0.0) for styling and argument parsing, respectively. Its last update was in 2014, making it unsuitable for modern projects due to outdated dependencies, lack of maintenance, and CommonJS-only module format. There is no active development or defined release cadence, and the current stable version is 1.1.27.
Common errors
-
ERR_REQUIRE_ESM: require() of ES Module ...cli-util' from ... not supported.
cause Attempting to import `cli-util` using ES module `import` syntax in a JavaScript module when `cli-util` is a CommonJS module.fixChange the import statement to CommonJS `require()` syntax: `const cli = require('cli-util');` -
TypeError: "some string".green is not a function
cause The `colors` library, which adds color methods to `String.prototype`, was not loaded or `cli-util` (which loads `colors`) was not required.fixEnsure `const cli = require('cli-util');` is called at least once in your application's lifecycle before attempting to use string color methods. Note that this package is abandoned, and `colors` modifies `String.prototype`.
Warnings
- breaking The package is abandoned since 2014 and is not maintained. It relies on extremely old versions of dependencies (e.g., `colors@0.6.2`, `commander@2.0.0`, `docopt@0.6.0`) that have known security vulnerabilities, outdated APIs, or adverse side effects (like `colors` modifying `String.prototype`). Using this package in modern applications is highly discouraged and poses security risks and compatibility issues.
- gotcha The `colors` dependency (version 0.6.2) included in this package modifies the global `String.prototype`. This means methods like `.red`, `.green`, `.yellow`, etc., become available on all string objects globally after `cli-util` is required. This can lead to unexpected behavior, conflicts with other libraries, or make code harder to reason about due to global state modification.
- gotcha This package is strictly CommonJS (CJS) and does not provide ES module (ESM) exports. Attempting to `import` it in an ESM context will result in a runtime error.
- deprecated The `commander` (version 2.0.0) and `docopt` (version 0.6.0) dependencies are severely outdated. Their APIs have changed significantly over many major versions since then, and they lack modern features, bug fixes, and security patches. Code relying on these old APIs will be incompatible with newer versions and potentially insecure.
Install
-
npm install cli-util -
yarn add cli-util -
pnpm add cli-util
Imports
- log
import { log } from 'cli-util';const cli = require('cli-util'); cli.log('message'); - colors
import colors from 'colors';
const cli = require('cli-util'); cli.log('Hello'.green); // Uses String.prototype modification from the internal 'colors' dependency - header
const { header } = require('cli-util'); // While technically correct, it's more idiomatic for CJS to assign the entire module.exports object.const cli = require('cli-util'); cli.header('My CLI Tool', { indent: 2 });
Quickstart
const cli = require('cli-util');
// Logging with colors (due to internal 'colors' dependency modifying String.prototype)
cli.log('Starting CLI application...'.green);
cli.warn('A warning message.'.yellow);
cli.error('An error occurred!'.red);
// Displaying a header
cli.header('CLI-Util Example', { indent: 1 });
// Formatting lines and columns
const data = [
['Name:', 'John Doe'],
['Email:', 'john.doe@example.com'],
['Status:', 'Active']
];
cli.lines(data.map(row => cli.pad(row[0], 10) + row[1]));
console.log(cli.line()); // Prints a separator line
// Example of padding
const paddedText = cli.rightPad('Hello', 15, '.');
cli.log(`Padded text: ${paddedText}`);
cli.ok('Application finished successfully.'.blue);