Option skip_lines_with_error

Table of Contents

The skip_lines_with_error option tolerates parsing errors. It skips the records containing an error inside and directly go process the next record.

  • Type: boolean
  • Optional
  • Default: false
  • Since: 1.0.6

Be careful, this functionality can not suit every data set. It implies a good knowledge in your data in the sense that you must be confident that no field contains any record delimiters. By nature, CSV fields can contains records delimiters if quoted. On error, the parser has no indication to know if a record delimiter is one or if it is inside a quoted field or not. Thus, using this option confidently implies that your fields do not contain any records delimiter inside.

A skip event is emitted when an error is found and when the record is skipped. The error object is passed as the first argument of the event callback and it can expose additional information depending on the type of errors. The error documentation list of the error types as well as the contextual properties they expose.

Listening to the skip event

The option.skip_lines_with_error.js example catch an invalid closing quote error and continue parsing the next records.

const parse = require('csv-parse')
const assert = require('assert')

parser = parse({
  skip_lines_with_error: true
}, function(err, records){
  assert.deepEqual(
    records, [
      ['a', 'b', 'c'],
      ['d', 'e', 'f'],
      ['h', 'i', 'j']
    ]
  )
})
parser.on( 'skip', function(err){
  assert(/^Invalid Closing Quote/.test(err.message))
})
parser.write(`
"a","b","c"
"d","e","f"
"invalid"," " ","record"
"h","i","j"
`.trim())
parser.end()