Parsing a CSV file using NodeJS Parsing a CSV file using NodeJS node.js node.js

Parsing a CSV file using NodeJS


Seems like you need to use a stream-based library such as fast-csv, which also includes validation support.


I used this way:-

var fs = require('fs'); var parse = require('csv-parse');var csvData=[];fs.createReadStream(req.file.path)    .pipe(parse({delimiter: ':'}))    .on('data', function(csvrow) {        console.log(csvrow);        //do something with csvrow        csvData.push(csvrow);            })    .on('end',function() {      //do something with csvData      console.log(csvData);    });


My current solution uses the async module to execute in series:

var fs = require('fs');var parse = require('csv-parse');var async = require('async');var inputFile='myfile.csv';var parser = parse({delimiter: ','}, function (err, data) {  async.eachSeries(data, function (line, callback) {    // do something with the line    doSomething(line).then(function() {      // when processing finishes invoke the callback to move to the next one      callback();    });  })});fs.createReadStream(inputFile).pipe(parser);