CLI Table Generator
cli-table is a Node.js utility for rendering structured, unicode-aided tables directly in the command line interface. As of its latest version 0.3.11, last published approximately four years ago, it provides extensive customization options including configurable characters for borders, adjustable column widths, text truncation, alignment (left, right, center), and padding. It also supports styling table headers through integration with the `colors.js` library. While its version number (0.3.x) and infrequent updates suggest it's a stable but not actively developed project, it remains a popular choice for producing visually organized output in CLI applications, evidenced by over 2.6 million weekly downloads. Its primary differentiator is its granular control over table aesthetics, allowing developers to create highly tailored visual presentations beyond simple CSV-like output.
Common errors
-
TypeError: Table is not a constructor
cause Attempting to use `import Table from 'cli-table'` in a Node.js environment where `cli-table` is detected as a CommonJS module and `import` is not correctly transpiled or configured for interop.fixChange the import statement to `const Table = require('cli-table');`. -
Table rows appear broken, misaligned, or text is unexpectedly truncated.
cause This often occurs due to incorrect `colWidths` being set, especially when column content includes invisible ANSI color codes or when `chars` configuration is malformed.fixVerify `colWidths` are sufficient for the content. If using ANSI colors, ensure they are handled correctly (e.g., stripped for length calculation). Double-check the `chars` object for correct unicode characters and complete definitions. Consider `cli-table3` for improved ANSI color handling. -
`Cannot read property 'toString' of undefined` or `undefined is not a function (near '...table.toString()...')`
cause Forgetting to call `.toString()` on the `cli-table` instance before attempting to log or manipulate its string representation. The `table` object itself is not a string.fixAlways call `console.log(table.toString());` or assign `table.toString()` to a variable.
Warnings
- gotcha The `cli-table` package (version 0.3.11) is a CommonJS module. Direct `import` statements in ES module contexts will result in `ERR_REQUIRE_ESM` errors unless specific Node.js loader configurations or transpilation are used.
- gotcha The `colors.js` dependency, while functional in the version `cli-table` uses, was subject to a highly publicized supply chain attack (self-sabotage) in January 2022. While `cli-table` itself wasn't directly compromised, transitive dependency resolution could theoretically pull in a problematic version if not carefully managed, or if `colors.js` has further incidents.
- gotcha Configuring the `chars` property for custom table borders can be verbose and error-prone. Small mistakes in character assignment or omission can lead to malformed or visually broken table layouts.
- gotcha Text containing ANSI color codes (e.g., from `chalk` or `colors.js` when applied to cell content) can cause `colWidths` calculations to be inaccurate, leading to misaligned columns or unexpected truncation. The invisible control characters are counted in length, but not rendered visibly.
- gotcha The package `cli-table` has not seen new versions released to npm in the past 12 months (as of early 2026) and can be considered a discontinued project with low attention from maintainers. For active development and new features, consider `cli-table3`, which is an actively maintained, API-compatible fork.
Install
-
npm install cli-table -
yarn add cli-table -
pnpm add cli-table
Imports
- Table
import Table from 'cli-table';
const Table = require('cli-table'); - Table (configured)
const Table = require('cli-table'); const table = new Table({ head: ['Header 1', 'Header 2'], colWidths: [20, 40] }); - Table (rows init)
const Table = require('cli-table'); const table = new Table({ rows: [ ['Row 1 Col 1', 'Row 1 Col 2'], ['Row 2 Col 1', 'Row 2 Col 2'] ] });
Quickstart
const Table = require('cli-table');
// Instantiate a new table with custom headers and column widths
const table = new Table({
head: ['Task ID', 'Description', 'Status', 'Due Date']
, colWidths: [10, 40, 15, 20] // Define column widths
, chars: {
'top': '═' , 'top-mid': '╤' , 'top-left': '╔' , 'top-right': '╗'
, 'bottom': '═' , 'bottom-mid': '╧' , 'bottom-left': '╚' , 'bottom-right': '╝'
, 'left': '║' , 'left-mid': '╟' , 'mid': '─' , 'mid-mid': '┼'
, 'right': '║' , 'right-mid': '╢' , 'middle': '│'
}
});
// Add rows to the table. Table is an Array-like object.
table.push(
[1, 'Implement feature A', 'In Progress', '2026-05-01']
, [2, 'Fix bug in module B that causes a critical error', 'Blocked', '2026-04-28']
, [3, 'Review pull request #123', 'Pending', '2026-04-25']
);
// Log the table to the console
console.log(table.toString());
// Example of a vertical table
const verticalTable = new Table();
verticalTable.push(
{ 'User': 'Alice' }
, { 'Role': 'Admin' }
, { 'Last Login': '2026-04-22 10:30' }
);
console.log('\nVertical Table:');
console.log(verticalTable.toString());