Node.js Quickies – Loading a CSV File

In this short tutorial I’m gonna show you how to load a CSV file using Node.js.

Firstly, you need to include the ya-csv package in your package.json file, under “dependencies”. Make sure you run “npm install” to download all your project’s dependencies.

"dependencies": {
   "ya-csv": "*"
},

 

This is how you can load a csv file:

var csv = require('ya-csv');

//load csv file
var loadCsv = function() {
    var reader = csv.createCsvFileReader('data/ActualizaSC.csv', {
      'separator': ',',
      'quote': '"',
      'escape': '"',       
      'comment': '',
  });

  var allEntries = new Array();

  reader.setColumnNames([ 'firstName', 'lastName', 'username' ]);

  reader.addListener('data', function(data) {
    //this gets called on every row load
    allEntries.push(data);
  });

  reader.addListener('end', function(data) {
    //this gets called when it's finished loading the entire file
    return allEntries;
  });
};

var myUsers = loadCsv();

You can learn more about this package by checking out it’s Github repo.

FREE COURSES
Python Blog Image

FINAL DAYS: Unlock coding courses in Unity, Godot, Unreal, Python and more.