{"id":12995,"library":"console-grid","title":"console-grid: Node.js Console Table Utility","description":"console-grid is a lightweight, zero-dependency utility for Node.js environments, designed to render highly customizable and formatted tables and grids directly in the console. Currently at version 2.2.4, it provides robust features such as tree-style rows for hierarchical data, precise column alignment (left, center, right), custom padding, and multi-line headers with automatic word wrapping. A key differentiator is its comprehensive support for colorful cells using ANSI escape codes, full Unicode character handling (including CJK and Emojis), and custom character width calculation via a `getCharLength` option. It offers dual support for both ESM and CommonJS modules and ships with TypeScript type definitions, making it suitable for modern JavaScript and TypeScript projects. While a specific release cadence isn't explicitly published, its stable nature as a core utility suggests infrequent but well-tested updates.","status":"active","version":"2.2.4","language":"javascript","source_language":"en","source_url":"https://github.com/cenfun/console-grid","tags":["javascript","typescript"],"install":[{"cmd":"npm install console-grid","lang":"bash","label":"npm"},{"cmd":"yarn add console-grid","lang":"bash","label":"yarn"},{"cmd":"pnpm add console-grid","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"CG is the default function export for rendering grids, available in both ESM and CJS.","wrong":"import { CG } from 'console-grid';","symbol":"CG","correct":"import CG from 'console-grid';"},{"note":"ConsoleGrid is a named class export, exclusively available via ESM imports since v2.x. It is not directly exposed through CommonJS require.","wrong":"const ConsoleGrid = require('console-grid').ConsoleGrid;","symbol":"ConsoleGrid","correct":"import { ConsoleGrid } from 'console-grid';"},{"note":"For CommonJS, the default export CG (the grid rendering function) is the primary way to interact with the library.","wrong":"const { ConsoleGrid } = require('console-grid');","symbol":"CG","correct":"const CG = require('console-grid');"}],"quickstart":{"code":"import CG from 'console-grid';\n\n// Example 1: Basic grid rendering\nconsole.log('--- Basic Grid ---');\nCG({\n    columns: ['', 'Name', 'Value'],\n    rows: [\n        [1, 'Tom', 'Value 1'],\n        [2, 'Jerry', 'Value 2']\n    ]\n});\n\n// Example 2: Grid without header\nconsole.log('\\n--- Grid Without Header ---');\nCG({\n    options: {\n        headerVisible: false\n    },\n    columns: ['', 'Name', 'Value'],\n    rows: [\n        [1, 'Tom', 'Value 1'],\n        [2, 'Jerry', 'Value 2']\n    ]\n});\n\n// Example 3: Grid with column width constraints and multi-line header\nconsole.log('\\n--- Grid with Column Widths and Multi-Line Header ---');\nCG({\n    columns: [\n        '',\n        { name: 'Name', minWidth: 15 },\n        { name: 'Value', maxWidth: 20 },\n        { name: 'Multiple Line Header', maxWidth: 15 }\n    ],\n    rows: [\n        [1, 'Hello', 'Long Text Value', 'Long Text Value'],\n        [2, 'Hello There', 'Long Text Value Long Text Value', 'Long Text Value Long Text Value']\n    ]\n});","lang":"typescript","description":"Demonstrates basic grid rendering, customizing header visibility, and configuring column widths, including support for multi-line headers."},"warnings":[{"fix":"Always use `import CG from 'console-grid';` for the default function in ESM, or `const CG = require('console-grid');` for CommonJS.","message":"When using ESM, the primary grid rendering function is the default export (CG), while the ConsoleGrid class is a named export. Importing `CG` as a named export (`import { CG } from 'console-grid';`) will result in a runtime error.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"If you require the `ConsoleGrid` class, your project must use ESM and import it with `import { ConsoleGrid } from 'console-grid';`.","message":"Attempting to access the `ConsoleGrid` class via CommonJS `require` (e.g., `require('console-grid').ConsoleGrid`) will result in `undefined` or a runtime error, as it's only exported as a named ESM export.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"Ensure your terminal supports full Unicode and ANSI escape codes. For specific character width issues, utilize the `getCharLength` option to provide a custom character width calculation function in the grid options.","message":"Table rendering may appear garbled, misaligned, or have incorrect column widths in certain terminal environments, especially when dealing with complex Unicode characters (e.g., CJK, emojis) or ANSI escape codes.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Always ensure ANSI escape codes are correctly formed and balanced. For debugging, try rendering the grid with `colorful: false` in the options to isolate the issue to color codes.","message":"While `console-grid` supports ANSI escape codes for colorful cells, improper use (e.g., unbalanced codes, unsupported sequences) can lead to unexpected rendering issues or terminal state corruption.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Change the import statement from `import { CG } from 'console-grid';` to `import CG from 'console-grid';`.","cause":"In ESM, the default export `CG` was imported as a named export.","error":"TypeError: CG is not a function"},{"fix":"If you need the `ConsoleGrid` class, convert your file to an ESM module (`.mjs` extension or `\"type\": \"module\"` in `package.json`) and use `import { ConsoleGrid } from 'console-grid';`.","cause":"Attempting to access the `ConsoleGrid` class using CommonJS `require()` syntax, but it is only exposed as a named ESM export.","error":"TypeError: Cannot destructure property 'ConsoleGrid' of 'require(...)' as it is undefined."},{"fix":"If `console-grid` is dual-packaged, ensure your project's `package.json` does not have `\"type\": \"module\"` if you intend to use `require()`. Otherwise, use `import CG from 'console-grid';` for ESM imports.","cause":"You are attempting to `require()` this package in a Node.js project configured as a pure ESM module, which prevents `require` calls.","error":"ERR_REQUIRE_ESM"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":""}