Handle CSV files without headers
masterheaders: false. In this mode, csv-parser uses the column index as the key for each column.repository·master·Indexed 23 days ago
https://github.com/mafintosh/csv-parserA high-performance streaming CSV parser for Node.js that converts CSV data into JSON objects. It aims for maximum speed and compatibility with the csv-spectrum test suite. The package provides a Transform stream for programmatic use and a CLI for converting CSV to newline-delimited JSON (NDJSON).
headers: false. In this mode, csv-parser uses the column index as the key for each column.You can install csv-parser using npm or yarn.
$ npm install csv-parser$ yarn add csv-parserIf your CSV file contains a leading Byte Order Mark (BOM), it may interfere with header parsing. Use strip-bom-stream in your pipeline to remove it.
const fs = require('fs');
const csv = require('csv-parser');
const stripBom = require('strip-bom-stream');
fs.createReadStream('data.csv')
.pipe(stripBom())
.pipe(csv())
...Pass an object to csv() to configure parsing behavior, such as the column separator.
csv({ separator: '\t' });To parse a CSV file, create a readable stream to the file, instantiate csv(), and pipe the stream into it. You can listen to the data event to process each row and the end event to know when parsing is complete.
const csv = require('csv-parser')
const fs = require('fs')
const results = [];
fs.createReadStream('data.csv')
.pipe(csv())
.on('data', (data) => results.push(data))
.on('end', () => {
console.log(results);
// [ { NAME: 'Daffy Duck', AGE: '24' }, { NAME: 'Bugs Bunny', AGE: '22' } ]
});The mapValues option accepts a function to transform the content of each column.
csv({
mapValues: ({ header, index, value }) => value.toLowerCase()
})csv() to specify custom headers. If you need to provide both headers and other configuration options, use the headers property within an options object.The mapHeaders option accepts a function to transform header names. This is useful for normalizing header casing or cleaning up names.
csv({
mapHeaders: ({ header, index }) => header.toLowerCase()
})The headers event is emitted after the header row is parsed. The callback receives an Array[String] containing the header names.
fs.createReadStream('data.csv')
.pipe(csv())
.on('headers', (headers) => {
console.log(`First header: ${headers[0]}`)
})When calling csv(opts), you can provide several configuration options to control parsing behavior:
| Option | Type | Default | Description |
|---|---|---|---|
headers | Array or boolean | null | An array of header names. If false, rows are returned as arrays of values indexed by number. |
mapHeaders | Function | ({ header }) => header | A function to transform header names. Receives { header, index }. |
mapValues | Function | ({ value }) => value | A function to transform cell values. Receives { header, index, value }. |
separator | string | ',' | The character used to separate cells. |
quote | string | '"' | The character used for quoting cells. |
escape | string | (same as quote) | The character used to escape quotes inside a quoted cell. |
newline | string | '\n' | The character used for newlines. |
skipComments | string or boolean | false | If a string, lines starting with this character are skipped. If true, defaults to #. |
skipLines | number | null | The number of lines to skip at the beginning of the file. |
strict | boolean | false | If true, emits an error if a row's column count doesn't match the header count. |
raw | boolean | false | If true, values are returned as Buffer objects instead of strings. |
maxRowBytes | number | Number.MAX_SAFE_INTEGER | Maximum allowed size for a single row in bytes. |
outputByteOffset | boolean | false | If true, each emitted object includes a byteOffset property. |
The csv() function accepts an options object with the following properties:
escape (String, default: "): Character used to escape strings.headers (Array[String] | Boolean): Specifies headers. If false, uses column indices. If no headers are provided, the first line is used.mapHeaders (Function): Modifies header values. Receives { header, index }.mapValues (Function): Modifies column values. Receives { header, index, value }.newline (String, default: \n): Character denoting the end of a line.quote (String, default: "): Character denoting a quoted string.raw (Boolean): If true, does not decode UTF-8 strings.separator (String, default: ,): Column separator character.skipComments (Boolean | String, default: false): If true, skips lines starting with #. If a string, uses that string as the comment prefix.skipLines (Number, default: 0): Number of lines to skip at the start of the file before parsing headers.maxRowBytes (Number, default: Number.MAX_SAFE_INTEGER): Max bytes per row before throwing an error.strict (Boolean, default: false): If true, throws an error if row column count doesn't match headers.outputByteOffset (Boolean, default: false): If true, emits rows as { byteOffset, row } where byteOffset is the start of the row in the stream.If you need to know the exact byte position in the source stream where a specific row starts, enable the outputByteOffset option. When enabled, the data event will emit an object containing both the row and the byteOffset.
const csv = require('csv-parser');
const fs = require('fs');
fs.createReadStream('data.csv')
.pipe(csv({ outputByteOffset: true }))
.on('data', ({ row, byteOffset }) => {
console.log(`Row at ${byteOffset}:`, row);
});