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
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. ThePapa.LocalChunkSize
,Papa.RemoteChunkSize
,download
,withCredentials
andworker
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 fromPapa.parse(Papa.NODE_STREAM_INPUT, options)
. ThePapa.LocalChunkSize
,Papa.RemoteChunkSize
,download
,withCredentials
,worker
,step
andcomplete
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)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With