CSVParse for Node.js


Async iterator

Async iterators provides an elegant method to iterate over each parsed records with the usage of the for await...of construct.

CSV parse rely on and leverages the Node.js stream readable API. It implements the Symbol.asyncIterator or Symbol.iterator iterable protocol.

This is kind of an obscure JS functionality and we don't have to deal with it. The end result is very comprehensive and illustrated with the async iterator example

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

import fs from 'node:fs';
import { parse } from 'csv-parse';

const __dirname = new URL('.', import.meta.url).pathname;

const processFile = async () => {
  const records = [];
  const parser = fs
    // CSV options if any
  for await (const record of parser) {
    // Work with each record
  return records;

(async () => {
  const records = await processFile();

Async iteration is also supported in CoffeeScript. It is expressed with the for...from syntax available since version 1.12.0. The async example in coffeescript is:

import fs from 'node:fs'
import { parse } from 'csv-parse'

const __dirname = new URL( '.', import.meta.url).pathname

processFile = () ->
  records = []
  parser = fs
  .createReadStream "#{__dirname}/fs_read.csv"
  .pipe parse(
    # CSV options if any
  for await record from parser
    # Work with each record

(() ->
  records = await processFile()
  console.info records


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