CSVParse for Node.js

IssuesGitHub

Promises usage

Starting with Node.js version 15, the Stream API promise a new "stream/promises" module.

Part of the Stream Promises module is the finished function. The Function plugs into a stream and resolves a promise when it is no longer readable, writable or has experienced an error or a premature close event.

The promises example leverages pipe as well as finished to provide a convenient solution to read a file from the file system and to pipe its output into the parser.

This example is available with the command node samples/recipe.promises.js.

const parse = require('csv-parse');
const fs = require('fs');
const { finished } = require('stream/promises');
 
const processFile = async () => {
  records = []
  const parser = fs
  .createReadStream(`./input.csv`)
  .pipe(parse({
    // CSV options if any
  }));
  parser.on('readable', function(){
    let record;
    while (record = parser.read()) {
      records.push(record)
    }
  });
  await finished(parser);
  return records
}

(async () => {
  const records = await processFile()
  console.info(records);
})()