Using Node.js to Write data to a CSV file

Posted by: Mahesh Sabnis , on 11/7/2015, in Category Node.js
Views: 21716
Abstract: Using the Node.js CSV module to persist non-relational data like CSV in a structured format

Web applications may need to store data in relational forms or in a file format. Which mechanism to use has to be decided carefully by the developer. If the incoming data already has a relational form, then it is better to store it in a relational database For e.g. Payroll Information, expense details etc. But if the data is loosely related, contains a looser consistency model or probably not suitable for a relational form, then in that case instead of using relational database, you can store it in a flat file or in a NoSQL database.

 

A flat file structure for storing data is one of the most elementary model of the database. The Comma Separated File (CSV), is very frequently used for managing data. In this case, data columns are separated using delimiters like a hyphen(-) or a semi-colon (;).

Node.js provides us Modules for writing and reading data to and from CSV. We have already seen how to read data in one of our previous articles http://www.dotnetcurry.com/nodejs/1204/read-csv-file-using-nodejs. Today we will see how to write data to a CSV file using Node.js.

Node.js Implementation

This application is developed using Visual Studio Code. This is a new FREE editor for building and debugging modern web and cloud applications. We also need Node.js tools so that we can install necessary modules. Please visit following links to grab the related information about Visual Studio Code.

https://code.visualstudio.com/Docs/editor/codebasics

https://code.visualstudio.com/Docs/runtimes/nodejs

Download Node tools from the following link https://github.com/Microsoft/nodejstools#readme

Step 1: To develop our application, we will create a folder structure which we can open in Visual Studio Code. Create a new folder of name VSCodeWriteCSVFile on any drive (C,D or E). In this folder create a Scripts folder, we will use this to write our JavaScript code. Now add a new datafile folder inside VSCodeWriteCSVFile folder to store our CSV.

Step 2: Open Visual Studio Code, and Open the VSCodeWriteCSVFile folder as shown in the following image.

openfile

This will open the VSCodeWriteCSVFile folder with all subfolders in it.

Step 3: To use the Node.js intellisense and installing the CSV module, open the Node Command Prompt. This is available if you have installed Node.js tools, as mentioned above. On this command prompt navigate to the VSCodeWriteCSVFile folder. Run the following command to install the TypeScript Definition Manager

npm install -g tsd

Here TypeScript Definition Manager (TSD) makes it easy to search the tsd files for the current folder workspace.

Run the following Command to install Node.js definitions, so that we can access the Node.js modules and use intellisense.

node-install

Run the following command to install the CSV Module

csvmodule

This will provide an access to the CSV module so that we can access the necessary functions to write data to a CSV file.

Step 4: Add a new file in the datafile folder of the name dataInfo.csv.

 

Step 5: In the scripts folder add a new file of name ‘app.js’. In this js file add the following script:

//1.
var csv = require('csv');
//2.
var obj = csv();
//3.
var dataInfo = [
    ['101', 'MS', 100000],
    ['102', 'LS', 80000],
    ['103', 'TS', 60000],
    ['104', 'VB', 200000],
    ['105', 'PB', 180000],
    ['106', 'AB', 160000]
];

//4.
obj.from.array(dataInfo).to.path('../datafile/dataInfo.csv');

The above code has the following specifications: (Numbering matches with the comment numbering in the above code.)

1. Load the CSV module to access all its functions.

2. The csv() function object is defined so that we can access the necessary methods.

3. Define the data array which will be written into the csv file.

4. from() and to() methods to read data from the data array and write it to the CSV in the specified path.

Step 6: Right click on the app.js and select the option of ’Open in Command Prompt’ as shown in the following image:

cmdprompt

This will open the command prompt, on this run the following command:

Node app.js

node-app

Now open the dataInfo.csv file from the datafile folder and you should be able to see the data written in it.

data-info-result

Conclusion

Node.js provides predefined modules with behavior, which helps to perform various operations easily through Node.js application. The CSV module is used to persist non-relational data in a structured format.

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

Author
Mahesh Sabnis is a DotNetCurry author and Microsoft MVP having over 17 years 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). Follow him on twitter @maheshdotnet


Page copy protected against web site content infringement 	by Copyscape




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

Categories

JOIN OUR COMMUNITY

POPULAR ARTICLES

FREE .NET MAGAZINES

Free DNC .NET Magazine

Tags

JQUERY COOKBOOK

jQuery CookBook