DEV Community

Cover image for Build a Universal CSV, Excel & JSON Data Previewer in Node.js
Abrar ahmed
Abrar ahmed

Posted on

Build a Universal CSV, Excel & JSON Data Previewer in Node.js

Ever received a file from a client and thought:

“Is this even readable?”
I used to dive into every CSV in Excel, every JSON in VSCode, and every XLSX in Google Sheets, just to see the first few rows. It was pretty exhausting!

So I built something better:
A simple Node.js tool to preview CSV, Excel, or JSON files directly from the terminal — no manual opening, no GUI.

This tutorial will walk you through how to build your own version of that.

What We’re Building

A terminal command like this:

node preview.js data.xlsx

Outputs this:

┌────────────┬─────────────┬────────────┐
│ Name │ Email │ Joined │
├────────────┼─────────────┼────────────┤
│ Alice │ [email protected] │ 2023-09-01 │
│ Bob │ [email protected] │ 2023-10-12 │
└────────────┴─────────────┴────────────┘

It will support:

  • CSV
  • Excel (.xlsx)
  • JSON And it’ll detect the type automatically — so you don’t need a flag.

What You’ll Use

  • fs (built-in)
  • path (built-in)
  • csv-parser
  • xlsx
  • cli-table3 (for formatted console output)

Setup

Create a folder:

mkdir data-previewer
cd data-previewer
npm init -y

Install dependencies:

npm install csv-parser xlsx cli-table3

Step 1: File Type Detection

Create file: preview.js

const fs = require('fs');
const path = require('path');

const filePath = process.argv[2];
if (!filePath) {
  console.error(' Please provide a file path.');
  process.exit(1);
}

const ext = path.extname(filePath).toLowerCase();
Enter fullscreen mode Exit fullscreen mode

This lets us detect whether the input file is .csv, .json or .xlsx.

Step 2: Parse CSV Files

Add this function to preview.js:

const csv = require('csv-parser');

function parseCSV(filePath, rowLimit = 5) {
  const results = [];
  fs.createReadStream(filePath)
    .pipe(csv())
    .on('data', (data) => {
      if (results.length < rowLimit) results.push(data);
    })
    .on('end', () => {
      renderTable(results);
    });
}
Enter fullscreen mode Exit fullscreen mode

Step 3: Parse Excel Files

Add:

const XLSX = require('xlsx');

function parseExcel(filePath, rowLimit = 5) {
  const wb = XLSX.readFile(filePath);
  const sheet = wb.Sheets[wb.SheetNames[0]];
  const json = XLSX.utils.sheet_to_json(sheet, { defval: '' });
  renderTable(json.slice(0, rowLimit));
}
Enter fullscreen mode Exit fullscreen mode

Step 4: Parse JSON Files

Add:

function parseJSON(filePath, rowLimit = 5) {
  const raw = fs.readFileSync(filePath, 'utf8');
  const data = JSON.parse(raw);
  const rows = Array.isArray(data) ? data : [data];
  renderTable(rows.slice(0, rowLimit));
}
Enter fullscreen mode Exit fullscreen mode

Step 5: Render the Table

Add:

const Table = require('cli-table3');

function renderTable(data) {
  if (!data || data.length === 0) {
    console.log(' No data found');
    return;
  }

  const headers = Object.keys(data[0]);
  const table = new Table({ head: headers });

  data.forEach(row => {
    table.push(headers.map(h => row[h]));
  });

  console.log(table.toString());
}
Enter fullscreen mode Exit fullscreen mode

Step 6: Glue It All Together

At the bottom of preview.js:

switch (ext) {
  case '.csv':
    parseCSV(filePath);
    break;
  case '.xlsx':
    parseExcel(filePath);
    break;
  case '.json':
    parseJSON(filePath);
    break;
  default:
    console.error(' Unsupported file type');
}
Enter fullscreen mode Exit fullscreen mode

Try It Out

Drop some sample files into your folder:

  • customers.csv
  • report.xlsx
  • data.json
Run:

node preview.js customers.csv
node preview.js report.xlsx
node preview.js data.json

Boom! You now have a simple, universal file preview tool.

Optional Upgrades

  • Limit row count via CLI arg: node preview.js data.csv 10
  • Highlight columns with missing values
  • Export preview to temp file (Markdown/HTML)
  • Add support for TSV or XML (fun challenge!)

Conclusion

This is a great project to build:

  • Your first CLI tool
  • Real-world file handling in Node.js
  • Practice with CSV, Excel, and JSON formats
  • Avoiding boilerplate GUI tools for file checks

If you found this useful, drop a like or bookmark.

Got improvements or want to extend it? Share your ideas below!

Top comments (0)