Read CSV File using Node JS
Mahesh K
Posted on August 9, 2018
I am currently working on a business project that exports the processed data to CSV file. I wanted to process that into the dashboard. I can choose python or any other backend language to read the CSV file. However, I have found out that node js may be much better option. I already have the node on the front end side. So I thought If I can process this using node. And make it available in the dashboard so that I don't have to waste much time with other language.
Here's simple code that got me started. You can also check out Read CSV with Node JS video for alternative explanation.
Check out the example code below that got me started. There's a lot more to be done to extend the below code. Like say if I wish to take the data from the mysql and export it to CSV then I have to change read to write operation.
const fs = require('fs');
const csv = require('csv-parser');
fs.createReadStream(inputFilePath)
.pipe(csv())
.on('data', function(data){
try {
//perform the operation
}
catch(err) {
//error handler
}
})
.on('end',function(){
//some final operation
});
Here's the CSV module that I have used. I think there are plenty of npm modules that you can check out. But the CSV parser seems to be simple and gets the job done.
What's your favorite node module for the CSV parsing?
Posted on August 9, 2018
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.