{"id":17420,"library":"cli-truncate","title":"CLI Truncate","description":"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.","status":"active","version":"6.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/sindresorhus/cli-truncate","tags":["javascript","truncate","ellipsis","text","limit","slice","cli","terminal","term","typescript"],"install":[{"cmd":"npm install cli-truncate","lang":"bash","label":"npm"},{"cmd":"yarn add cli-truncate","lang":"bash","label":"yarn"},{"cmd":"pnpm add cli-truncate","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The package has been pure ESM since v3.0.0. CommonJS `require()` is not supported.","wrong":"const cliTruncate = require('cli-truncate');","symbol":"cliTruncate","correct":"import cliTruncate from 'cli-truncate';"},{"note":"TypeScript types are included and automatically picked up when importing.","symbol":"cliTruncate (TypeScript)","correct":"import cliTruncate from 'cli-truncate';"},{"note":"For type-only imports in TypeScript, use the `import type` syntax.","symbol":"Options (TypeScript Type)","correct":"import type { Options } from 'cli-truncate';"}],"quickstart":{"code":"import cliTruncate from 'cli-truncate';\n\nconst 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.';\n\n// Truncate a long string to the current terminal width, or 80 columns if not available.\nconst terminalWidth = process.stdout.columns ?? 80;\nconst truncatedText = cliTruncate(paragraph, terminalWidth, { position: 'end', preferTruncationOnSpace: true });\n\nconsole.log('Original length:', paragraph.length);\nconsole.log('Truncated length (visual):', truncatedText.length);\nconsole.log('Truncated text:', truncatedText);","lang":"typescript","description":"Demonstrates truncating a long paragraph to the current terminal width, gracefully handling character widths and potential ANSI escape codes, for clean console output. It also shows using `preferTruncationOnSpace` for better readability."},"warnings":[{"fix":"Upgrade your Node.js environment to version 22 or higher, or downgrade `cli-truncate` to a compatible major version (e.g., v5 for Node.js 20, v4 for Node.js 18).","message":"Node.js 22 or later is now required for `cli-truncate` v6.0.0.","severity":"breaking","affected_versions":">=6.0.0"},{"fix":"Update your import statements from `const cliTruncate = require('cli-truncate');` to `import cliTruncate from 'cli-truncate';`. Ensure your project is configured for ESM, potentially by adding `'type': 'module'` to your `package.json` or by using `.mjs` file extensions.","message":"`cli-truncate` migrated to pure ESM (ECMAScript Modules). CommonJS `require()` is no longer supported.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Upgrade your Node.js environment to version 20 or higher, or downgrade `cli-truncate` to v4 if you need Node.js 18 support.","message":"Node.js 20 or later is required for `cli-truncate` v5.0.0.","severity":"breaking","affected_versions":">=5.0.0 <6.0.0"},{"fix":"Upgrade your Node.js environment to version 18 or higher, or downgrade `cli-truncate` to v3 if you need Node.js 12.20+ support.","message":"Node.js 18 or later is required for `cli-truncate` v4.0.0.","severity":"breaking","affected_versions":">=4.0.0 <5.0.0"},{"fix":"Be aware of this specific styling behavior if precise style continuity for the ellipsis in `position: 'middle'` is critical to your output design.","message":"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.","severity":"gotcha","affected_versions":">=5.1.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Change 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.","cause":"Attempting to use `require()` to import `cli-truncate`, which is an ES Module.","error":"ERR_REQUIRE_ESM: require() of ES Module /path/to/node_modules/cli-truncate/index.js from /your/project/file.js not supported."},{"fix":"Upgrade 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).","cause":"Running `cli-truncate` v6.0.0 (or a similar version) with an older Node.js runtime.","error":"The 'cli-truncate' package is not compatible with your current Node.js version. Required: '>=22'"}],"ecosystem":"npm","meta_description":null}