npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

prettytable

v2.0.0

Published

CLI based Node module for generating pretty tables from multiple data sources

Readme

PrettyTable

npm version Downloads License: MIT JavaScript Style Guide Build Status Maintenance

A CLI based Node.js module for generating and displaying pretty ASCII tables from multiple data sources.

  • 📊 Generate tables from arrays, CSV, or JSON data
  • 🔍 Sort, filter, and manipulate table data
  • 🖨️ Output as plain text or HTML
  • 🛠️ Modern JavaScript with comprehensive error handling

Installation

npm install prettytable

Basic Usage

Create a table with headers and rows in one shot:

// Import and instantiate
const PrettyTable = require("prettytable");
const pt = new PrettyTable();

// Define headers and data
const headers = ["Name", "Age", "City"];
const rows = [
  ["John", 22, "New York"],
  ["Elizabeth", 43, "Chicago"],
  ["Bill", 31, "Atlanta"],
  ["Mary", 18, "Los Angeles"],
];

// Create and display table
pt.create(headers, rows);
pt.print();

Output:

+-----------+-----+-------------+
| Name      | Age | City        |
+-----------+-----+-------------+
| John      | 22  | New York    |
| Elizabeth | 43  | Chicago     |
| Bill      | 31  | Atlanta     |
| Mary      | 18  | Los Angeles |
+-----------+-----+-------------+

Build a table row-by-row:

const PrettyTable = require("prettytable");
const pt = new PrettyTable();

// Set column names
pt.fieldNames(["City", "Area", "Population", "Rainfall"]);

// Add rows one by one
pt.addRow(["Adelaide", 1295, 1158259, 600.5]);
pt.addRow(["Brisbane", 5905, 1857594, 1146.4]);
pt.addRow(["Darwin", 112, 120900, 1714.7]);
pt.addRow(["Hobart", 1357, 205556, 619.5]);
pt.addRow(["Sydney", 2058, 4336374, 1214.8]);
pt.addRow(["Melbourne", 1566, 3806092, 646.9]);
pt.addRow(["Perth", 5386, 1554769, 869.4]);

// Display the table
pt.print();

Data Import Features

Import from CSV

const PrettyTable = require("prettytable");
const pt = new PrettyTable();

// Load from CSV file (first row is used as headers)
pt.csv("./data.csv");
pt.print();

Import from JSON

const PrettyTable = require("prettytable");
const pt = new PrettyTable();

// Load from JSON file (object keys become headers)
pt.json("./data.json");
pt.print();

Output Options

Return as String

Get the table as a formatted string:

const PrettyTable = require("prettytable");
const pt = new PrettyTable();

pt.fieldNames(["Name", "Age", "City"]);
pt.addRow(["John", 25, "New York"]);
pt.addRow(["Mary", 30, "Chicago"]);

const tableString = pt.toString();
console.log(tableString);

Generate HTML

Create an HTML table representation:

const PrettyTable = require("prettytable");
const pt = new PrettyTable();

pt.fieldNames(["Name", "Age", "City"]);
pt.addRow(["John", 25, "New York"]);
pt.addRow(["Mary", 30, "Chicago"]);

// Basic HTML table
const basicHtml = pt.html();

// HTML table with custom attributes
const styledHtml = pt.html({
  id: "data-table",
  class: "table table-striped",
  border: "1",
});

Table Manipulation

Sort by Column

const PrettyTable = require("prettytable");
const pt = new PrettyTable();

pt.fieldNames(["Name", "Age", "City"]);
pt.addRow(["John", 22, "New York"]);
pt.addRow(["Elizabeth", 43, "Chicago"]);
pt.addRow(["Bill", 31, "Atlanta"]);
pt.addRow(["Mary", 18, "Los Angeles"]);

// Sort by age (ascending)
pt.sortTable("Age");
pt.print();

// Sort by name (descending)
pt.sortTable("Name", true);
pt.print();

Delete and Modify Data

const PrettyTable = require("prettytable");
const pt = new PrettyTable();

pt.fieldNames(["Name", "Age", "City"]);
pt.addRow(["John", 22, "New York"]);
pt.addRow(["Elizabeth", 43, "Chicago"]);
pt.addRow(["Bill", 31, "Atlanta"]);
pt.addRow(["Mary", 18, "Los Angeles"]);

// Delete the second row (Elizabeth)
pt.deleteRow(2);
pt.print();

// Clear all rows but keep the structure
pt.clearTable();
pt.print();

// Delete the entire table
pt.deleteTable();

Error Handling

PrettyTable includes comprehensive error handling for all methods:

const PrettyTable = require("prettytable");
const pt = new PrettyTable();

try {
  pt.fieldNames(["Name", "Age", "City"]);
  pt.addRow(["John", 22]); // Error: Row length doesn't match columns
} catch (error) {
  console.error(`Error: ${error.message}`);
}

API Reference

The PrettyTable module provides the following methods:

| Method | Description | | ------------------------------ | ---------------------------------- | | fieldNames(array) | Define column headers | | addRow(array) | Add a single row | | create(headers, rows) | Create table with headers and rows | | toString() | Return table as a formatted string | | print() | Print table to console | | html([attributes]) | Generate HTML representation | | csv(filename) | Import data from CSV file | | json(filename) | Import data from JSON file | | sortTable(column, [reverse]) | Sort by column | | deleteRow(rownum) | Delete a specific row | | clearTable() | Remove all rows | | deleteTable() | Delete entire table |

Contributing

This project welcomes contributions! The codebase uses ESLint for code quality and Mocha/Chai for testing.

# Install dependencies
npm install

# Run linter
npm run lint

# Fix automatically fixable issues
npm run lint:fix

# Run tests
npm test

License

This project is licensed under the MIT License - see the LICENSE file for details.