Using Node.js to Write data to a CSV file
Posted by: Mahesh Sabnis
in Category Node.js
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 https://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.
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.
Download Node tools from the following link https://github.com/Microsoft/nodejstools#readme
Step 2: Open Visual Studio Code, and Open the VSCodeWriteCSVFile folder as shown in the following image.
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.
Run the following command to install the CSV Module
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:
var csv = require('csv');
var obj = csv();
var dataInfo = [
['101', 'MS', 100000],
['102', 'LS', 80000],
['103', 'TS', 60000],
['104', 'VB', 200000],
['105', 'PB', 180000],
['106', 'AB', 160000]
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:
This will open the command prompt, on this run the following command:
Now open the dataInfo.csv file from the datafile folder and you should be able to see the data written in it.
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.
This article has been editorially reviewed by Suprotim Agarwal.
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 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 eBook 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 .NET Standard and the upcoming C# 8.0 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!