Working with CSV in JavaScript (Node.js): Libraries and Practical Examples
CSV (Comma-Separated Values) is one of the most common formats for exchanging tabular data. In the Node.js ecosystem, there are several mature libraries that allow you to efficiently read, process, and create CSV files. In this article, we will review the main tools, their capabilities, and provide practical code examples.
Popular Libraries for Working with CSV
1. csv-parser — Fast Streaming Parser
This library is the standard for reading CSV in Node.js. It operates in stream mode, allowing you to process files of any size without loading them entirely into memory.
Installation:
npm install csv-parserExample of reading a CSV file:
const fs = require('fs');const csv = require('csv-parser');
const results = [];
fs.createReadStream('data.csv') .pipe(csv()) .on('data', (row) => { results.push(row); }) .on('end', () => { console.log('CSV successfully read'); console.log(results.slice(0, 3)); // First 3 records });Key Features:
- Support for custom delimiters (comma by default)
- Automatic header detection from the first row
- Ability to skip empty rows
- Low memory consumption due to streaming processing
2. csv-stringify — Creating CSV from Objects
This tool is part of the csv family and is designed for generating CSV strings or streams from JavaScript objects.
Installation:
npm install csv-stringifyExample of writing data to CSV:
const { stringify } = require('csv-stringify');const fs = require('fs');
const data = [ { name: 'Anna', age: 28, city: 'Moscow' }, { name: 'Ivan', age: 35, city: 'Saint Petersburg' }, { name: 'Olga', age: 22, city: 'Kazan' }];
const writableStream = fs.createWriteStream('output.csv');const stringifier = stringify({ header: true });
stringifier.pipe(writableStream);data.forEach(row => stringifier.write(row));stringifier.end();
console.log('File output.csv created');Capabilities:
- Automatic escaping of special characters (quotes, commas)
- Configurable column delimiter
- Support for custom headers
- Works in both synchronous and asynchronous modes
3. csv-writer — Simple and Intuitive API
This library offers a simpler interface for writing CSV, especially when working with small amounts of data.
Installation:
npm install csv-writerExample of writing:
const createCsvWriter = require('csv-writer').createObjectCsvWriter;
const csvWriter = createCsvWriter({ path: 'users.csv', header: [ { id: 'id', title: 'ID' }, { id: 'name', title: 'Name' }, { id: 'email', title: 'Email' } ]});
const records = [ { id: 1, name: 'Alexey', email: 'alex@example.com' }, { id: 2, name: 'Maria', email: 'maria@example.com' }];
csvWriter.writeRecords(records) .then(() => console.log('CSV file created'));Advanced Use Cases
Processing Large Files with Data Transformation
Let's combine reading and writing in a single stream to process data on the fly:
const fs = require('fs');const csv = require('csv-parser');const { stringify } = require('csv-stringify');
const readStream = fs.createReadStream('input.csv');const writeStream = fs.createWriteStream('output_transformed.csv');const stringifier = stringify({ h