Skip to content

Instantly share code, notes, and snippets.

@paambaati
Created June 15, 2023 10:21
Show Gist options
  • Save paambaati/f950149f4105a0c881fd6c19bf456e33 to your computer and use it in GitHub Desktop.
Save paambaati/f950149f4105a0c881fd6c19bf456e33 to your computer and use it in GitHub Desktop.
Parse CSV as a stream in Node.js
import { createReadStream } from 'fs';
import { NODE_STREAM_INPUT, parse } from 'papaparse';
(async () => {
const csvStream = createReadStream('machine-readable-business-employment-data-mar-2023-quarter.csv');
const parseStream = parse(NODE_STREAM_INPUT);
csvStream.pipe(parseStream);
const data: Array<Array<string>> = [];
parseStream.on('data', (chunk: Array<string>) => {
data.push(chunk);
});
parseStream.on('finish', () => {
console.log(data);
console.log(data.length);
});
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment