34

I am trying to read a csv file using node js. Her is my code

fs.readFile(config.csvUploadPath, function read(err, data) {
    if (err) {
        throw err;
    }
    console.log(data + 'my data')
});

CONSOLE:

ID
D11
D33
D55

Here I want to get the elements in the column ID and store those in an array. How can I do that? Can anyone suggest me help. Thanks. My controller:

var partnersModel = new partners(params);
        fs.readFile(config.csvUploadPath, function read(err, data) {
            if (err) {
                throw err;
            }
        dataArray = data.toString().split(/\r?\n/);
            dataArray.forEach(function(v,i){
                if(v !== 'DUI'){
                  partnersModel.dui.push(v);
                }
            });
        });
        partnersModel.save(function(error, response){
7

3 Answers 3

52

Use a library, CSV has lots of gotchas. I have come to enjoy the package csv. It is located here: https://www.npmjs.com/package/csv . Here is a very quick example using the async api.

const fs = require('fs')
var parse = require('csv-parse')
fs.readFile(inputPath, function (err, fileData) {
  parse(fileData, {columns: false, trim: true}, function(err, rows) {
    // Your CSV data is in an array of arrys passed to this callback as rows.
  })
})

Since your file does not have multiple values per row and contains no delimiters besides newline, it is only trivially CSV. Maybe String.prototype.split() is for you?

const fs = require('fs')
fs.readFile(inputPath, 'utf8', function (err, data) {
  var dataArray = data.split(/\r?\n/);  //Be careful if you are in a \r\n world...
  // Your array contains ['ID', 'D11', ... ]
})
7
  • CSV files typically have multiple values per row, so it returns an array of arrays... one for each row. For your example rows= [ ['id'], ['D11'], ['D33'], ['D55'] ]. Note that the header was parsed too, and that to index one of the values you would need to use something like rows[1][0]. Jan 21, 2017 at 8:24
  • There are options about header handling, and the library can help you stuff the values into objects for each row with properties matching the column names. TFM is located here: csv.adaltas.com/parse Jan 21, 2017 at 8:25
  • But i am getting the error' data.split is not a function'
    – MMR
    Jan 21, 2017 at 11:09
  • I thought readFile always passed a string as data, but apparently it only does if you specify an encoding. The example has been updated! Jan 21, 2017 at 11:14
  • i got it by this... dataArray = data.toString().split(/\r?\n/);
    – MMR
    Jan 21, 2017 at 11:22
18

I used a stream, fs, and csv-parse like in this answer:

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

const data = []
fs.createReadStream(filename)
  .pipe(parse({ delimiter: ',' }))
  .on('data', (r) => {
    console.log(r);
    data.push(r);        
  })
  .on('end', () => {
    console.log(data);
  })
3
  • 1
    How do I access the data in the array? for some reason (i think it because of the async nature of createReadStream) the array lentght equals 0, when I try to check it
    – user9629064
    Jun 14, 2022 at 11:49
  • parse is not defined. Nov 26, 2022 at 19:19
  • @KylePennell You have to const csv = require('csv-parse') which imports the whole module and then use the parse method of that with csv.parse(...
    – timlg07
    Oct 10 at 10:51
0

From How to read data From *.CSV file using javascript?, use the jQuery-CSV library.

Note: The library is designed to handle any CSV data that is RFC 4180 compliant, including all of the nasty edge cases that most 'simple' solutions overlook.

var fs = require('fs');
var $ = jQuery = require('jquery');
$.csv = require('jquery-csv');

var sample = './path/to/data/sample.csv';
fs.readFile(sample, 'UTF-8', function(err, csv) {
  $.csv.toArrays(csv, {}, function(err, data) {
    for(var i=0, len=data.length; i<len; i++) {
      console.log(data[i]); //Will print every csv line as a newline
    }
  });
});

code snippet from jquery-csv's examples here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.