CLI Table 3: Pretty Unicode Tables
cli-table3 is an active utility library for Node.js environments, providing a robust mechanism to render highly customizable unicode-aided tables directly in the command line. As of version 0.6.5, it offers features such as cell spanning (both column and row), individual cell styling (borders, padding), flexible text alignment, word wrapping with configurable boundaries, and enhanced handling of ANSI color characters within cell content. It is the maintained successor and API-compatible evolution of the unmaintained `cli-table` and `cli-table2` packages. Releases are consistent, addressing bug fixes and introducing enhancements like hyperlink support, `bigint` cell content, and improved TypeScript types. It ships with comprehensive TypeScript type definitions, making it suitable for modern JavaScript and TypeScript projects.
Common errors
-
TypeError: Table is not a constructor
cause Attempting to use named import syntax (`import { Table } from 'cli-table3';`) for the main `Table` class, which is a default export.fixUse the correct default import syntax: `import Table from 'cli-table3';`. -
Error: cli-table3 requires Node.js version 10.* || >= 12.*
cause Running `cli-table3` on an unsupported Node.js version, specifically older than 10.x or between 10.x and 12.x.fixUpgrade your Node.js runtime to version 10.* or >= 12.*. You can use tools like `nvm` (Node Version Manager) to manage multiple Node.js versions. -
Property 'href' does not exist on type 'CellOptions'
cause Attempting to use the `href` property within `CellOptions` (for hyperlink support) with an older version of `cli-table3` or outdated TypeScript type definitions.fixEnsure you are using `cli-table3` version 0.6.2 or higher, as `href` support was added in this version. Update your package and regenerate `node_modules/@types` if necessary.
Warnings
- breaking Node.js version requirements were updated in v0.5.0. Projects using older Node.js runtimes (prior to 10.* or 12.*) will encounter errors.
- gotcha cli-table3 is the maintained successor to the unmaintained `cli-table` and `cli-table2` packages. While API compatible, always install `cli-table3` to receive ongoing updates and bug fixes.
- breaking The initial release of `cli-table3` (v0.4.0) introduced breaking changes compared to `cli-table2` (e.g., removal of 'lodash' dependency, internal refactors). While API compatible at a high level, direct migration from `cli-table2` might require minor adjustments if relying on specific internals or removed dependencies.
Install
-
npm install cli-table3 -
yarn add cli-table3 -
pnpm add cli-table3
Imports
- Table
import { Table } from 'cli-table3';import Table from 'cli-table3';
- Table.TableOptions
import { Table, TableOptions } from 'cli-table3';import Table, { TableOptions } from 'cli-table3'; - CellOptions
import { CellOptions } from 'cli-table3';import type { CellOptions } from 'cli-table3';
Quickstart
import Table, { TableOptions, CellOptions } from 'cli-table3';
interface MyRow {
header1: string | CellOptions;
header2: string | CellOptions;
}
const tableConfig: TableOptions = {
head: ['Project', 'Status', 'Progress'],
colWidths: [30, 15, 20],
wordWrap: true,
style: {
'header': ['cyan', 'bold'],
'border': ['grey'],
'compact': true
}
};
const table = new Table(tableConfig);
// Add rows
table.push(
['CLI Table 3', { content: 'Active', style: { fg: 'green' } }, '100%'],
['Feature A', 'In Development', { content: '60%', style: { fg: 'yellow' } }],
['Feature B', { content: 'On Hold', style: { fg: 'red' } }, '10%'],
[{ content: 'Total tasks: 3', colSpan: 3, hAlign: 'center' }]
);
console.log(table.toString());
// Example of a vertical table
const verticalTable = new Table();
verticalTable.push(
{ 'Name': 'Alice' },
{ 'Age': '30' },
{ 'City': 'New York' }
);
console.log('\n--- Vertical Table ---\n');
console.log(verticalTable.toString());