Easy Table
easy-table is a JavaScript utility for rendering plain text tables primarily for command-line interfaces. It allows for highly customizable table structures, including dynamic cell content formatting via 'printer' functions, column sorting with specific syntax, and aggregation features like totaling and averaging. The package is currently at version 1.2.0. Its last known activity on npm was 5 years ago, and the GitHub repository shows an 8-year inactivity period, suggesting it is in an abandoned state rather than actively maintained. It ships with TypeScript type definitions, enabling type-safe usage in modern TypeScript projects. Key differentiators include its flexible cell rendering pipeline and built-in aggregation capabilities for simple CLI reporting.
Common errors
-
TypeError: Table is not a constructor
cause Attempting to instantiate `Table` using an incorrect import, typically `import { Table } from 'easy-table'` instead of `import Table from 'easy-table'` in ESM, or `require('easy-table').Table` in CommonJS if it's a default export.fixFor ESM, use `import Table from 'easy-table'`. For CommonJS, ensure you're doing `const Table = require('easy-table')` if `Table` is the default/module.exports object. -
TypeError: t.cell is not a function
cause `t` is not an instance of `Table`. This often happens if `new Table()` was not called, or the import of `Table` was incorrect, resulting in `t` being undefined or an unexpected object.fixEnsure `let t = new Table()` is called after correctly importing `Table`. -
ReferenceError: require is not defined
cause Attempting to use `require('easy-table')` in an ECMAScript Module (ESM) context (e.g., a file with `"type": "module"` in `package.json` or a `.mjs` file).fixSwitch to ESM import syntax: `import Table from 'easy-table'`. If you must use `require` in an ESM file, Node.js v22+ has some experimental support, or consider `await import('easy-table')` for older versions, or explicitly configure `type: 'commonjs'` in your `package.json`. -
Table does not sort correctly or throws an error for column 'XYZ'
cause The string passed to `t.sort(['Column Name'])` does not exactly match a column name or uses an invalid sort order suffix (e.g., 'asc' vs 'ass').fixVerify that the column name string passed to `t.sort()` is an exact match for one of the column headers used in `t.cell()`. Ensure sort order suffixes are strictly `|asc` or `|des`.
Warnings
- breaking The package appears to be abandoned, with no new features, bug fixes, or security updates for over 5 years. Relying on it for critical applications may pose risks.
- gotcha The package primarily uses CommonJS module syntax (`require`). While it ships with TypeScript types, direct ESM `import { Table } from 'easy-table'` will likely fail in strict ESM environments because it's a default export from a CommonJS module.
- gotcha Custom cell 'printer' functions require explicit handling of the `width` parameter for correct padding. If `width` is not handled, content might not be correctly aligned or truncated when the library calculates layout.
- gotcha Sorting columns by string-based syntax (e.g., `['Column Name|des']`) is specific and can be prone to typos. No validation is provided for column names or sort orders (e.g., 'des' vs 'descending').
Install
-
npm install easy-table -
yarn add easy-table -
pnpm add easy-table
Imports
- Table (CommonJS)
const Table = require('easy-table') - Table (ESM)
import { Table } from 'easy-table'import Table from 'easy-table'
- Table.print
import { print } from 'easy-table'; print(data)import Table from 'easy-table'; Table.print(data)
Quickstart
import Table from 'easy-table';
const data = [
{ id: 123123, desc: 'Something awesome', price: 1000.00 },
{ id: 245452, desc: 'Very interesting book', price: 11.45},
{ id: 232323, desc: 'Yet another product', price: 555.55 }
];
// Using instance methods for fine-grained control
let t = new Table();
data.forEach(product => {
t.cell('Product Id', product.id);
t.cell('Description', product.desc);
t.cell('Price, USD', product.price, Table.number(2)); // Format price to 2 decimal places
t.newRow();
});
console.log('Formatted Table:\n' + t.toString());
// Using the static print method for quick rendering with options
console.log('\nAuto-generated Table with options:\n' + Table.print(data, {
desc: { name: 'Product Description' }, // Rename column
price: { printer: Table.number(2) } // Apply printer
}));
// Example of sorting and totaling
let sortableTable = new Table();
data.forEach(product => {
sortableTable.cell('Product Id', product.id);
sortableTable.cell('Description', product.desc);
sortableTable.cell('Price, USD', product.price);
sortableTable.newRow();
});
sortableTable.sort(['Price, USD|des']); // Sort by Price descending
sortableTable.total('Price, USD', { printer: Table.aggr.printer('Total: ', Table.number(2)) });
console.log('\nSorted and Totaled Table:\n' + sortableTable.toString());