CSVParse for Node.js

IssuesGitHub

Sync API

The sync API exposes a function, which expects as input a full dataset as text, and which returns the full result set as an array or an object.

To summarise, it is a regular direct synchronous call to a function: you pass CSV content and it returns records. Because of its simplicity, this is the recommended approach if you don't need scalability and if your dataset fit in memory.

Choose the sync API for simplicity, readability and convenience at the expense of not being scalable.

Import or require the csv-parse/sync module to use it. The exported function signature is const records = parse(data, [options]).

The synchronous example illustrates how to use the synchronous module. This example is available with the command node samples/api.sync.js.

import assert from 'node:assert';
import { parse } from 'csv-parse/sync';

const input = `
"key_1","key_2"
"value 1","value 2"
`;
const records = parse(input, {
  columns: true,
  skip_empty_lines: true
});
assert.deepStrictEqual(
  records,
  [{ key_1: 'value 1', key_2: 'value 2' }]
);

About

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