Search code examples
csvreactjspapaparse

Read local CSV with relative path in ReactJS


With reactjs (in Meteor) I would like to read a local csv file with a relative path and loop over it's rows:

import Papa from 'papaparse';
var csvfile = "../../../data.csv";
Papa.parse(csvfile, {
  step: function (row) {
    console.log("Row:", row.data);
  },
});

This returns

Row: [ [ '../../../data.csv' ] ]


Solution

  • Looks like your library expect JSON, whereas you specified a path.

    You should read the JSON from your file using fs and then pass that to the parse method.

    fs.readFile(csvFile, 'utf8', function (err, data) {
        if (err) {
            throw err;
        }
        Papa.parse(data, {
         step: function (row) {
          console.log("Row:", row.data);
         }
       });
    });