Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use papaparse on Node JS?

I have been trying to get papaparse running on nodejs for a very long time now and I keep failing. I want to try to load my local CSV and then give this papaparse? How to do that? My code does not work.

import papa from "papaparse";
import fs from "fs";

export const convertCSV = async (res: Response) => {
  const file = await fs.createReadStream("../files/test.csv");
  papa.parse(file, {
    header: true,
    complete: function (results, file) {
      console.log("Complete", results.data.length, "records.");
    },
  });
};

results.data.length is always 0.

My CSV file is located in the files folder, which is located in the src folder:

src/files/test.csv
like image 512
redlightfilms Avatar asked Sep 15 '25 20:09

redlightfilms


1 Answers

From the Papaparse README, it supports Node in this way:

Papa Parse can parse a Readable Stream instead of a File when used in Node.js environments (in addition to plain strings). In this mode, encoding must, if specified, be a Node-supported character encoding. The Papa.LocalChunkSize, Papa.RemoteChunkSize, download, withCredentials and worker config options are unavailable.

Papa Parse can also parse in a node streaming style which makes .pipe available. Simply pipe the Readable Stream to the stream returned from Papa.parse(Papa.NODE_STREAM_INPUT, options). The Papa.LocalChunkSize, Papa.RemoteChunkSize, download, withCredentials, worker, step and complete config options are unavailable. To register a callback with the stream to process data, use the 'data' event like so: stream.on('data', callback) and to signal the end of stream, use the 'end' event like so: stream.on('end', callback).

like image 102
Erick Petrucelli Avatar answered Sep 18 '25 12:09

Erick Petrucelli