CSV for Node.js


Node.js stream API

The Node.js stream API is scalable and offers the greatest control over the data flow.

Using the pipe API

Pipes in Node.js is a native functionnality provided by the stream API. It behave just like Unix pipes where the output of a process, here a stream reader, is redirected as the input of the following process, here a stream writer.

The pipe example is quite readable while also scalable:

// Import the package
import * as csv from 'csv';

// Run the pipeline
  // Generate 20 records
    delimiter: '|',
    length: 20
  // Transform CSV data into records
    delimiter: '|'
  // Transform each value into uppercase
  .pipe(csv.transform((record) =>
    record.map((value) =>
  // Convert objects into a stream
    quoted: true
  // Print the CSV stream to stdout

Using the native stream functions

The native stream functions provide flexibity but comes at the cost of being more verbose and harder to write. Data is consumed inside the readable event with the stream.read function. It is then written by calling the stream.write function. The stream example illustrates how to initialize each packages and how to plug them.

// Import the package
import {generate, parse, transform, stringify} from 'csv';

let i = 0;

const generator = generate({seed: 1, columns: 2, length: 20});
const parser = parse();
const transformer = transform(function(data){
  return data.map(function(value){return value.toUpperCase();});
const stringifier = stringify();

// Read generated CSV data and send it to the parser
generator.on('readable', function(){
  let data; while((data = generator.read()) !== null){
// When generation is over, close the parser
generator.on('end', function(){

// Read parsed records and send them to the transformer
parser.on('readable', function(){
  let data; while((data = parser.read()) !== null){
// When parsing is over, close the transformer
parser.on('end', function(){

// Read transformed records and send them to the stringifier
transformer.on('readable', function(){
  let data; while((data = transformer.read()) !== null){
// When transformation is over, close the stringifier
transformer.on('end', function(){

// Read CSV data and print it to stdout
stringifier.on('readable', function(){
  let data; while((data = stringifier.read()) !== null){
// When stringifying is over, print a summary to stderr
generator.on('close', function(){
  process.stderr.write('=> ' + i + ' records\n');


The Node.js CSV project is an open source product hosted on GitHub and developed by Adaltas.