CLI Simple Table
cli-simple-table is a lightweight utility for generating basic, unstyled ASCII tables in command-line interfaces. Its design prioritizes simplicity and a minimal API, making it suitable for quick console output without extensive configuration. The library allows users to define table headers, add rows, and configure column-specific options such as text alignment and maximum width for truncation. It provides a `toString()` method to render the table for printing. The current stable version is `1.1.1`, released in late 2021, indicating a moderate release cadence. It differentiates itself from more feature-rich table rendering libraries by offering a focused solution for common CLI display needs. The package ships with TypeScript types, ensuring good developer experience for TypeScript projects, and supports both CommonJS and ES Module environments.
Common errors
-
TypeError: cli_simple_table is not a constructor
cause Attempting to instantiate `cli-simple-table` with an incorrect import (e.g., `new cli_simple_table.SimpleTable()` or `new (require('cli-simple-table')).default()`) when the module itself is the constructor.fixEnsure you are importing and instantiating the module directly: `const SimpleTable = require('cli-simple-table'); new SimpleTable();` or `import SimpleTable from 'cli-simple-table'; new SimpleTable();`. -
ERR_REQUIRE_ESM
cause Trying to `require()` the `cli-simple-table` package in an environment where it's being resolved as an ES Module, and the CommonJS `main` entry point is not correctly picked up or is being overridden.fixVerify your `package.json`'s `type` field, module resolution settings in bundlers, and ensure the correct entry point (`main` for CJS, `module` for ESM) is being used. If you are in an ESM project, prefer `import SimpleTable from 'cli-simple-table';`.
Warnings
- breaking Version `1.1.0` introduced an ES Module (ESM) entry point. While CommonJS support was maintained, projects relying solely on `require()` might need to ensure correct import paths or adjust bundler configurations if they previously relied on specific internal module resolution patterns.
- gotcha Using an incorrect import style (e.g., named import when a default is expected, or vice-versa) can lead to runtime errors like 'is not a constructor' or 'undefined'. The library primarily exports a default `SimpleTable` class.
- gotcha Prior to `v1.1.1`, there were edge cases in TypeScript strict mode that could lead to type-related issues. While fixed, older versions might exhibit unexpected behavior in highly strict TypeScript environments.
Install
-
npm install cli-simple-table -
yarn add cli-simple-table -
pnpm add cli-simple-table
Imports
- SimpleTable
const { SimpleTable } = require('cli-simple-table');const SimpleTable = require('cli-simple-table'); - SimpleTable
import { SimpleTable } from 'cli-simple-table';import SimpleTable from 'cli-simple-table';
- SimpleTable
import SimpleTable = require('cli-simple-table');
Quickstart
import SimpleTable from 'cli-simple-table';
import chalk from 'chalk'; // chalk is an external dependency
const table = new SimpleTable();
table.header('Name', 'Emoji');
table.row(chalk.red('Squid'), '🦑');
table.row(chalk.green('Frog'), '🐸');
table.row(chalk.yellow('Tiger'), '🐯');
table.row(chalk.blue('Whale'), '🐳');
table.row(chalk.magenta('Unicorn'), '🦄');
table.row(chalk.cyan('Dolphin'), '🐬');
table.row(chalk.white('Panda'), '🐼');
console.log(table.toString());