Read a CSV file using Node.js

Posted by: Mahesh Sabnis , on 10/23/2015, in Category Node.js
Views: 40045
Abstract: Use Node.js CSV module to easily read data stored on the server side and make it available to the client on request.

Several formats can be used for persisting data. If the data is not relational in nature, we can store it in flat files. Flat files was one of the most preferred form of data persistence in legacy apps, and continues to be in many modern apps as well. Comma Separated Files (CSV) is one such medium where data can be stored and used later.

Node.js provides CSV module using which we can Read/Write from and to CSV files. In this article, we will read a CSV file and expose the data as JSON so that any client application e.g. jQuery, Angular, DataTableJs, etc. can consume it.


Node.js CSV Reader Implementation

You can use any IDE of your choice like Sublime Text or the free Visual Studio Code. If you are using Visual Studio Code, you also need NTVS (Node Tools for Visual Studio). Here’s an entire article on the same Node Tools for Visual Studio.

Note: This application is developed using Visual Studio Code. This is a new editor by Microsoft for building and debugging modern web and cloud applications.

Step 1: To develop our application, we will create a folder structure which we will later open in Visual Studio Code. Create a new folder of name VSCodeReadCSVFile on your hard drive. Add a new subfolder datafile inside VSCodeWriteCSVFile. In the datafile folder, add a new file and call it datainfi.csv. Add the following data in it:


Step 2: Open Visual Studio Code and open the VSCodeReadCSVFile folder as shown in the following image:


Step 3: In this folder, add a Scripts folder. We will use this to create an application JavaScript file.


Step 4: In the Scripts folder add a new file of name app.js as shown in the following image


Step 5: Navigate to the VSCodeReadCSVFile folder from the Node Command prompt and initialize the TypeScript Definition for Node.js intellisense as shown in the following image:

tsd query node --action install



Step 6: Install the CSV node module using the following command from the Node Command prompt


Step 7: Open app.js from the scripts folder and add the following code in it:

var csv = require('csv');
var obj = csv();
function Employee(eno, ename, sal) {

    this.EmpNo = eno;
    this.EmpName = ename;
    this.Salary = sal;

var Employees = [];

obj.from.path('../datafile/dataInfo.csv').to.array(function (data) {
    for (var index = 0; index < data.length; index++) {
        Employees.push(new Employee(data[index][0], data[index][1], data[index][2]));
var http = require('http');
var server = http.createServer(function (req, resp) {
    resp.writeHead(200, { 'content-type': 'application/json' });



The above code does the following (The numbering matches with the Comment numbering in the above code.)

1. Loads the CSV module so that we can perform Read operation.

2. Get the CVS () function object to access required functionality.

3. Define the Employee object with parameterized constructor. We will be using this object for storing the data read from the CSV file into an array of Employee.

4. Employee array will contain the data from the CSV file, this will be sent to the clients request over http.

5. reads the CSV file from the path and the data is stored in an array using callback function. The function iterates through an array and each line record from the CSV file will be pushed as a record in an Employees array.

6. Load the http module.

7. Create a web server with request listener callback. This write the response header with the content type as json and also end the response by sending the Employees array in the JSON form.

8. The web server start listening the on the 5050 port.

Step 8: Right click on app.js. Select the option Open in Command Prompt. In this command prompt, enter the following command

Node app

This will show the following result


Step 9: Open any browser and enter the following URL in address bar http://localhost:5050

..and you should see the following result


Now we can create a client application using jQuery, Angular, etc. and consume the data to show it on HTML page.


Using the Node.js CSV module, we can easily read data stored on the server side and make it available to the client on request.

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Mahesh Sabnis is a DotNetCurry author and a Microsoft MVP having over two decades of experience in IT education and development. He is a Microsoft Certified Trainer (MCT) since 2005 and has conducted various Corporate Training programs for .NET Technologies (all versions), and Front-end technologies like Angular and React. Follow him on twitter @maheshdotnet or connect with him on LinkedIn

Page copy protected against web site content infringement 	by Copyscape

Feedback - Leave us some adulation, criticism and everything in between!