Support Online
Skip to main content

Node.js CSV Reading and Writing (with node-csv)

What Will You Learn in This Guide?

This guide explains how to process CSV files efficiently with Node.js.
You learn stream-based reading and writing methods for large files.

Technical Summary

  • Subject: Node.js CSV operations
  • Problem: Memory overflow with large CSV files
  • Solution: use node-csv + stream
  • Goal: Scalable data processing

How to Use CSV Files in Node.js?

CSV files are plain text files that store tabular data.
Fields are usually separated by commas.

There are many CSV libraries for Node.js.
node-csv is suitable for large files with its stream-based structure.


Preparing the Project Environment

mkdir csv_demo
cd csv_demo
npm init -y
npm install csv sqlite3
  • These commands install the project folder and necessary packages.

Using CSV Modules


const { parse } = require("csv-parse");
const { stringify } = require("csv-stringify");
  • These modules are for CSV reading and writing operations.

Reading CSV File (via Stream)


const fs = require("fs");
const { parse } = require("csv-parse");

fs.createReadStream("data.csv")
.pipe(parse({ delimiter: ",", from_line: 2 }))
.on("data", (row) => {
console.log(row);
});
  • This code reads the CSV file line by line.

Reading CSV as Object


fs.createReadStream("data.csv")
.pipe(parse({ columns: true, trim: true }))
.on("data", (row) => {
console.log(row.year_month);
});
  • This setting uses the first line as the title.

Secure Reading with async/await


async function oku() {
const parser = fs
.createReadStream("data.csv")
.pipe(parse({ columns: true }));

for await (const row of parser) {
console.log(row);
}
}
  • This method prevents backpressure problems.

Writing to CSV File


const { stringify } = require("csv-stringify");
const fs = require("fs");

const stringifier = stringify({ header: true });
stringifier.pipe(fs.createWriteStream("output.csv"));

stringifier.write({ name: "Ali", age: 30 });
stringifier.end();
  • This code writes JSON data to CSV.

Generating CSV from Database


db.each("SELECT * FROM users", (err, row) => {
stringifier.write(row);
});
  • This method exports data without storing it in memory.

stream.pipeline For Large Files


const { pipeline } = require("stream/promises");

await pipeline(
fs.createReadStream("input.csv"),
parse({ columns: true }),
stringify({ header: true }),
fs.createWriteStream("output.csv")
);
  • This structure makes error management automatic.

CSV → JSON Conversion


fs.createReadStream("data.csv")
.pipe(parse({ columns: true }))
.pipe(JSONStream.stringify())
.pipe(fs.createWriteStream("data.json"));
  • This method is suitable for large files.

JSON → CSV Conversion


Readable.from(jsonArray)
.pipe(stringify({ header: true }))
.pipe(fs.createWriteStream("out.csv"));
  • This code converts JSON string to CSV.

  1. Managing Inaccurate Data
  • Keep log with line number

  • Write the incorrect lines to a separate file

  • Do not stop the application


Frequently Asked Questions

1. node-csv or fast-csv? node-csv offers a more flexible structure.

2. Does it block large files? No, it does not block if stream is used.

3. What should I do if the delimiter is different? delimiter: ";" Set like .

4. Can I read CSV without a library? Not recommended, edge-cases cause problems.


Result

With node-csv, CSV files are processed securely and scalably. The Stream approach keeps memory usage to a minimum.

You can easily use this structure when processing high volumes of data on GenixNode.