Skip to main content
How To Read CSV File Using Noidejs

How To Read CSV File Using Noidejs

This tutorial will help you learn how to read CSV data using nodejs and csv-parse module. CSV files are text files that contain comma-separated data.

CSV stands for comma-separated-values is the most popular file format to exchange information or data between cross-programming languages.

The CSV format is flexible and language-independent, so it can be processed using any programming language.

I have already shared tutorials Reading csv file using JavaScript and HTML5 and jQuery to read csv data into HTML table.

CSV Parse Module

The csv-parse module is a dependable CSV parser that has been used by Node developers for a number of years. It is a part of the Node.js library set known as the node-csv module, which is used to manipulate CSV files.

How To read CSV file

You will also need the native fs module to get a file from the file system. Let’s create a file app.js:

The sample CSV data

Let’s assume, We have a test.csv file with the following content:

Name, Age, Salary
Adam, 42, 12345
Brad, 31, 6543
Rachel, 22 , 4356

Step 1: install csv-parse on your project with npm:

npm install csv-parse

Step 2: import both fs and csv-parse modules into your nodejs app.js file:

const fs = require("fs");
const { parse } = require("csv-parse");

Created instances of fs and csv-parse module.

Step 3: You need to use the fs.createReadStream() function to create a readable stream in order to read a CSV file using csv-parse.

fs.createReadStream("./test.csv");

Replace the ./test.csv parameter with the path to your CSV file.

Step 4: The parse() function should receive the stream data as an input, as shown below.

fs.createReadStream("./test.csv")
.pipe(parse({ delimiter: ",", from_line: 2 }))
.on("data", function (row) {
console.log(row);
})
.on("error", function (error) {
console.log(error.message);
})
.on("end", function () {
console.log("finished");
});

The .on("data") event is where your CSV file’s rows will be read one by one.

You’ll get the following output when you run the app.js file using node:

D:\workspace\nodejs_workspace\csv-read>node app.js
[ 'Adam', ' 42', ' 12345' ]
[ 'Brad', ' 31', ' 6543' ]
[ 'Rachel', ' 22 ', ' 4356' ]
finished

How To Create JSON Object From a CSV file

You can generate an object for each CSV row using cs-parser as well, we just need to change the options passed to the parse() method.

As we know the first row contains all column names, So this row(header) will be used as the keys.

.parse({
delimiter: ",",
columns: true,
ltrim: true,
})
  • columns: Column names will be considered on the first line, and values will be considered on succeeding lines.
  • ltrim: is used to trim the whitespace from your data.

Sample Code To Read CSV in JSON Format

const data = [];
fs.createReadStream("./test.csv")
.pipe(
parse({
delimiter: ",",
columns: true,
ltrim: true,
})
)
.on("data", function (row) {
// 👇 push the object row into the array
data.push(row);
})
.on("error", function (error) {
console.log(error.message);
})
.on("end", function () {
// 👇 log the result array
console.log("Successfully: The csv data has been pasred.");
console.log(data);
});
  • create an empty array variable named data at the top of the file.
  • We have pushed each row into the above data array.
  • log the success and error messages into the console.

Output:

D:\workspace\nodejs_workspace\csv-read>node app.js
Successfully: The csv data has been pasred.
[
{ Name: 'Adam', Age: '42', Salary: '12345' },
{ Name: 'Brad', Age: '31', Salary: '6543' },
{ Name: 'Rachel', Age: '22 ', Salary: '4356' }
]

Leave a Reply

Your email address will not be published. Required fields are marked *