CLI Column Formatter
cli-columns is a lightweight Node.js utility designed to format arrays of strings into neatly aligned, columnated lists suitable for command-line interfaces. It is particularly noted for its robust handling of both Unicode characters and ANSI escape codes (e.g., for colored text) by accurately calculating visible widths, preventing visual misalignment common in other simpler columnizers. The current stable version is 4.0.0, which notably addressed a ReDoS vulnerability in its upstream dependencies and updated its minimum Node.js requirement to version 10 or higher. While its release cadence may appear measured, the project prioritizes stability and precise rendering for CLI output. Key differentiators include customizable padding characters, column spacing, newline characters, optional sorting, and the ability to set a maximum output width, often defaulting to `process.stdout.columns`.
Common errors
-
ReferenceError: require is not defined
cause Attempting to use `require()` syntax in an ES Module context (e.g., a file with `"type": "module"` in `package.json` or a `.mjs` file).fixChange the import statement to `import columns from 'cli-columns';` in your ES module file. -
TypeError: cli_columns is not a function
cause Incorrectly importing the `columns` function using named import syntax (`import { columns } from 'cli-columns';`) when it's exported as a default.fixUse the correct default import syntax: `import columns from 'cli-columns';`. -
Error: The 'values' argument must be an Array<String>
cause Providing a non-array or an array containing non-string elements to the `columns` function. The library expects an array of strings.fixEnsure the argument passed to `columns()` is an array where all elements are strings. For example: `columns(['string1', 'string2'])`.
Warnings
- breaking Version 4.0.0 of `cli-columns` now requires Node.js version 10 or greater. Projects running on older Node.js versions must upgrade their environment or stick to `cli-columns@3.x`.
- breaking Version 4.0.0 includes updated dependencies to address an upstream Regular Expression Denial of Service (ReDoS) vulnerability. Older versions of `cli-columns` (prior to 4.0.0) may be exposed to this vulnerability through their transitive dependencies.
Install
-
npm install cli-columns -
yarn add cli-columns -
pnpm add cli-columns
Imports
- columns
import { columns } from 'cli-columns';import columns from 'cli-columns';
- columns
const { columns } = require('cli-columns');const columns = require('cli-columns'); - columns
import * as cliColumns from 'cli-columns'; const columns = cliColumns.default;
import columns from 'cli-columns';
Quickstart
import columns from 'cli-columns';
import chalk from 'chalk';
const values = [
'blue' + chalk.bgBlue('berry'),
'笔菠萝' + chalk.yellow('苹果笔'),
chalk.red('apple'), 'pomegranate',
'durian', chalk.green('star fruit'),
'パイナップル', 'apricot', 'banana',
'pineapple', chalk.bgRed.yellow('orange'),
'long-long-item-that-will-wrap-if-width-is-too-small'
];
// Basic usage, uses process.stdout.columns for width
console.log('--- Default Columnation ---');
console.log(columns(values));
// Custom width and no sorting
console.log('\n--- Custom Width (40) & Unsorted ---');
console.log(columns(values, { width: 40, sort: false }));
// Custom padding character
console.log('\n--- Custom Padding Character (".") ---');
console.log(columns(values, { character: '.' }));