CLI Truncate
cli-truncate is a JavaScript library designed for precisely truncating strings within a terminal environment, correctly accounting for the visual width occupied by characters, including those with ANSI escape codes (like colors and styles), Unicode surrogate pairs, and fullwidth characters. The current stable version is 6.0.0, which requires Node.js 22.x or later. The package has a consistent release cadence, often introducing new major versions to align with updated Node.js engine requirements, alongside minor and patch releases for bug fixes and feature enhancements. Its key differentiators lie in its robust handling of terminal-specific challenges: it ensures that styled text, such as output from `chalk`, is truncated accurately without breaking formatting, and it correctly measures the width of complex Unicode characters that standard `String.prototype.slice` or similar methods would misinterpret. It offers flexible truncation positions (start, middle, end) and options like `space` for adding a gap before the ellipsis and `preferTruncationOnSpace` to attempt truncation at a word boundary for better readability.
Common errors
-
ERR_REQUIRE_ESM: require() of ES Module /path/to/node_modules/cli-truncate/index.js from /your/project/file.js not supported.
cause Attempting to use `require()` to import `cli-truncate`, which is an ES Module.fixChange your import statement to `import cliTruncate from 'cli-truncate';`. Ensure your `package.json` has `'type': 'module'` or use `.mjs` file extension for your source file. -
The 'cli-truncate' package is not compatible with your current Node.js version. Required: '>=22'
cause Running `cli-truncate` v6.0.0 (or a similar version) with an older Node.js runtime.fixUpgrade your Node.js installation to version 22 or higher. Alternatively, install an older major version of `cli-truncate` that is compatible with your current Node.js version (e.g., `npm install cli-truncate@5` for Node.js 20).
Warnings
- breaking Node.js 22 or later is now required for `cli-truncate` v6.0.0.
- breaking `cli-truncate` migrated to pure ESM (ECMAScript Modules). CommonJS `require()` is no longer supported.
- breaking Node.js 20 or later is required for `cli-truncate` v5.0.0.
- breaking Node.js 18 or later is required for `cli-truncate` v4.0.0.
- gotcha When using ANSI escape codes (e.g., with `chalk`), `cli-truncate` attempts to inherit the style for the truncation character. For `position: 'middle'`, the truncation character does not inherit surrounding ANSI styles.
Install
-
npm install cli-truncate -
yarn add cli-truncate -
pnpm add cli-truncate
Imports
- cliTruncate
const cliTruncate = require('cli-truncate');import cliTruncate from 'cli-truncate';
- cliTruncate (TypeScript)
import cliTruncate from 'cli-truncate';
- Options (TypeScript Type)
import type { Options } from 'cli-truncate';
Quickstart
import cliTruncate from 'cli-truncate';
const paragraph = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris.';
// Truncate a long string to the current terminal width, or 80 columns if not available.
const terminalWidth = process.stdout.columns ?? 80;
const truncatedText = cliTruncate(paragraph, terminalWidth, { position: 'end', preferTruncationOnSpace: true });
console.log('Original length:', paragraph.length);
console.log('Truncated length (visual):', truncatedText.length);
console.log('Truncated text:', truncatedText);