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-parseVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic stream-based CSV parsing with a custom delimiter, error handling, and record collection.
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.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.
Always attach your final processing logic (e.g., verifying `records` array content) to the `parser.on('end', ...)` event, not `parser.on('finish', ...)`.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.
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.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+).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.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', ...)`.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.
No dependency data recorded yet.