Read CSV File using Node JS
Mahesh K

Mahesh K @maheshkale

About: I write (about) code and keep interest in Data science, Venture Funding and startups.

Joined:
May 2, 2018

Read CSV File using Node JS

Publish Date: Aug 9 '18
19 12

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
});  
Enter fullscreen mode Exit fullscreen mode

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?

Comments 12 total

  • ItsASine (Kayla)
    ItsASine (Kayla)Aug 9, 2018

    I just use fs.

    require('fs');
    // ...
    readFile: function(path) {
        var fileContent;
    
        return new Promise(function(resolve) {
            fileContent = fs.readFileSync(path, {encoding: 'utf8'});
            resolve(fileContent);
        });
    }
    

    When I use the contents of the file, it's like

    stuff,stuff,stuff,stuff,stuff\r\n
    thing,thing,thing,thing,thing\r\n
    thing,thing,thing,thing,thing\r\n
    thing,thing,thing,thing,thing\r\n
    thing,thing,thing,thing,thing\r\n
    thing,thing,thing,thing,thing
    

    which is good enough for my purposes.

    • Ang3h Courage
      Ang3h CourageJan 7, 2019

      typo: fileContent = fs.readFileSync(path, {encoding: 'utf8'});

    • Isa Levine
      Isa LevineSep 1, 2019

      Hi Kayla, this is awesome! Do you mind if I link to this code snippet in a CSV-parsing dev.to article I'm working on? I'd love to include a purely fs solution. :)

  • Fabio Franchino
    Fabio FranchinoAug 10, 2018

    I wrote an article a while ago possibly related: fabiofranchino.com/blog/merging-cs...

    • Mahesh K
      Mahesh KAug 10, 2018

      This is handy for sure. Thanks a lot.

  • Brian Greig
    Brian GreigAug 13, 2018

    I generally prefer to use something like csvtojson and get it into a format that is easier to parse.

  • TahorSuiJuris
    TahorSuiJurisMay 28, 2019

    Question, please:

    Why does this only return the first character of the string?

        const searchkeywords = fs.readFileSync('kwords.csv','utf-8');
            for (let kword of searchkeywords) {
        console.log(`Search Keyword: ${kword}`);
    

    Text for the first 10 rows is:

    "1-800-FLOWERS.COM, INC.",
    "1ST SOURCE",
    "1ST SOURCE CORP",
    "3D SYSTEMS",
    "8X8, INC.",
    "A.H. BELO",
    "AAON",
    "AARON RENTS",
    "ABERCROMBIE & FITCH",
    "ABIOMED",

  • Steve Hsu
    Steve HsuMar 2, 2020

    Thanks! It saved me.

Add comment