Registry / data / csv-parse

csv-parse

JSON →
library6.2.1jsnpmunverified

csv-parse is a robust and flexible CSV parsing library for both Node.js and web environments, currently at version 6.2.1. It efficiently converts CSV text input into arrays or objects. A core feature is its implementation of the Node.js `stream.Transform` API, enabling scalable processing of large datasets with minimal memory footprint. For simpler use cases, it also offers convenient callback-based and synchronous APIs. Key differentiators include its extensive options for handling various CSV formats (delimiters, quotes, escapes, comments, line breaks, etc.), multiple distribution targets (Node.js, Web, ESM, CJS), a long and stable history since its initial release in 2010, and a strong focus on complete test coverage. The package is part of the larger `csv` project and integrates seamlessly with related packages like `csv-generate` and `csv-stringify`. It maintains a regular release cadence with ongoing development and support from Adaltas, making it a reliable choice for CSV parsing needs.

npm install csv-parse
INSTALL
IMPORT
SIG · CSV-PARSE
C
csv-parse
datajavascriptv6.2.1
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

parse
✓ import { parse } from 'csv-parse'
✗ import parse from 'csv-parse'
For streaming or callback-based parsing in ESM. Since v5, all imports are named exports; default imports are no longer supported.
parse (sync)
✓ import { parse } from 'csv-parse/sync'
✗ import { parse } from 'csv-parse/lib/sync'
For synchronous parsing in ESM. The path changed from `csv-parse/lib/sync` to `csv-parse/sync` in v5.
parse
✓ const { parse } = require('csv-parse')
✗ const parse = require('csv-parse')
For streaming or callback-based parsing in CommonJS. Although `require('csv-parse')` works, destructuring is recommended for consistency with ESM.
parse (sync)
✓ const { parse } = require('csv-parse/sync')
✗ const parse = require('csv-parse/lib/sync')
For synchronous parsing in CommonJS. The path for the sync module changed from `csv-parse/lib/sync` to `csv-parse/sync` in v5.
Options
✓ import type { Options } from 'csv-parse'
Type import for configuration options in TypeScript.
parse (browser ESM)
✓ import { parse } from 'csv-parse/browser/esm'
✗ import { parse } from 'csv-parse'
For browser-specific ESM builds. Directly importing from 'csv-parse' might pull Node.js polyfills, leading to issues.

Demonstrates basic stream-based CSV parsing with a custom delimiter, error handling, and record collection.

import assert from "assert"; import { parse } from "csv-parse"; const records = []; // Initialize the parser with a custom delimiter const parser = parse({ delimiter: ":", // Using `columns: true` to output objects instead of arrays // (though in this example, fixed column names are not specified) }); // Use the readable stream API to consume records asynchronously parser.on("readable", function () { let record; while ((record = parser.read()) !== null) { records.push(record); } }); // Catch any parsing or stream errors parser.on("error", function (err) { console.error("Parsing error:", err.message); }); // Test that the parsed records matched the expected records parser.on("end", function () { assert.deepStrictEqual(records, [ ["root", "x", "0", "0", "root", "/root", "/bin/bash"], ["someone", "x", "1022", "1022", "", "/home/someone", "/bin/bash"], ]); console.log("CSV parsing complete and successful!"); }); // Write data to the stream - for a file, you'd pipe fs.createReadStream here parser.write("root:x:0:0:root:/root:/bin/bash\n"); parser.write("someone:x:1022:1022::/home/someone:/bin/bash\n"); // Close the writable stream to signal no more data will be written parser.end();
Debug
Known issues
breakingVersion 5 of `csv-parse` introduced breaking changes related to ECMAScript Modules (ESM) migration. CommonJS consumers need to update import paths for sync modules from `require('csv-parse/lib/sync')` to `require('csv-parse/sync')`. All imports for `parse` (stream/callback) and `parse/sync` (synchronous) are now named exports, meaning `import { parse } from 'csv-parse'` is correct, while `import parse from 'csv-parse'` will fail.
fix
For ESM, use named imports: `import { parse } from 'csv-parse'`. For CJS sync API, change `require('csv-parse/lib/sync')` to `require('csv-parse/sync')`. Review the documentation for specific paths if you encounter module resolution errors.
affects: >=5.0.0
breakingSeveral options were renamed in `csv-parse` v5 to improve clarity and consistency. Examples include `relax` renamed to `relax_quotes`, `skip_lines_with_empty_values` to `skip_records_with_empty_values`, and `skip_lines_with_error` to `skip_records_with_error`. Error codes were also renamed, e.g., `CSV_RECORD_DONT_MATCH_COLUMNS_LENGTH` became `CSV_RECORD_INCONSISTENT_COLUMNS`.
fix
Update your parsing options and error handling logic to use the new names. Consult the `csv-parse` API documentation for the full list of renamed options and error codes.
affects: >=5.0.0
gotchaIncorrect handling of stream events can lead to incomplete data. The `finish` event is emitted when the writable stream has flushed all its input data, but it does not mean all *parsed records* have been consumed from the readable stream. To ensure all records are processed, you must use the `end` event of the readable stream.
fix
Always attach your final processing logic (e.g., verifying `records` array content) to the `parser.on('end', ...)` event, not `parser.on('finish', ...)`.
affects: >=3.0.0
gotchaMalformed CSV data can lead to parsing errors. Common issues include unescaped delimiters within text fields, unescaped double quotes within quoted strings, inconsistent delimiters, or inconsistent row lengths.
fix
Ensure your CSV data adheres to standard formatting. Use options like `relax_quotes: true` or `relax_column_count: true` (since v5, formerly `relax` and `relax_column_count` respectively) to make the parser more tolerant of minor inconsistencies, but be aware this might obscure actual data quality issues. For unescaped quotes, ensure data is pre-processed or `escape` option is correctly configured.
affects: >=3.0.0
gotchaWhen working with different character encodings (e.g., UTF-8, ISO-8859-1), incorrect or unspecified encoding can lead to garbled text or parsing errors, especially with special characters.
fix
Always specify the correct `encoding` option if your CSV file is not UTF-8. For example, `parse({ encoding: 'latin1' })`. If reading from a file, ensure the `fs.createReadStream` also uses the correct encoding.
affects: >=3.0.0
Errors
Common errors & fixes
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: ...
Attempting to `require()` an ESM module from `csv-parse` in a CommonJS context (Node.js versions that enforce ESM for `.js` files when `"type": "module"` is present in `package.json`), or when using the wrong import path for CommonJS.
fix
For ESM, use `import { parse } from 'csv-parse'` or `import { parse } from 'csv-parse/sync'`. For CommonJS, ensure you are using the correct `require` paths, e.g., `const { parse } = require('csv-parse')` or `const { parse } = require('csv-parse/sync')` (note the change from `/lib/sync` in v5+).
Error: Cannot find module 'buffer' (or '_stream_readable.js:46 Uncaught Error: Cannot find module 'buffer'')
This typically occurs in browser environments when a Node.js-specific module (like `buffer` or stream polyfills) is expected but not available or correctly bundled. This often happens when importing the Node.js distribution of `csv-parse` into a browser project without proper polyfilling/bundling setup.
fix
For browser environments, use the specific browser ESM or IIFE builds provided by the library. For example, `import { parse } from 'csv-parse/browser/esm'` if using a module bundler like Webpack. Ensure your bundler is configured to correctly handle Node.js polyfills if you must use the Node.js distribution in the browser.
Parse Error: CSV_INVALID_CLOSING_QUOTE
An unescaped quote character was found at an unexpected location, usually within a quoted field, causing the parser to incorrectly terminate the field. This also includes an opening quote not being closed by the end of the data.
fix
Inspect the CSV data for malformed quoted fields. If the data quality is inconsistent, consider setting the `relax_quotes` option to `true` (formerly `relax`) to make the parser more tolerant, or ensure the `escape` option is correctly configured if a custom escape character is used. If the error code is `CSV_INVALID_OPENING_QUOTE` try to `parser.on('end', ...)` instead of `parser.on('close', ...)`.
Parse Error: CSV_RECORD_INCONSISTENT_FIELDS_LENGTH
A record (row) in the CSV has a different number of fields (columns) than previous records, which violates the expected consistent structure.
fix
Verify the CSV data for structural consistency. If variable column counts are expected, set the `relax_column_count` option to `true` (available since v3, also `relax_column_count` was `relax_column_count` pre-v5). If `columns` option is enabled and a record doesn't match the defined columns, the error `CSV_RECORD_DONT_MATCH_COLUMNS_LENGTH` (now `CSV_RECORD_INCONSISTENT_COLUMNS`) may occur.
Upgrade
Version history
6.2.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
9 hits · last 30 days
node
8
OpenAI (training)
1
Resources
csv-parse — npm install csv-parse · libregistry