- Published on
Working with CSV files in Nodejs
Overview
Setting up the project
To get started, create a directory csv_demo
and navigate into it:
mkdir csv_demo
cd csv_demo
Next initialize the project with the following command:
npm init -y
With -y
flag npm init
will pick all the default options for the prompts.
Now install the csv
package:
npm install csv
Following is the sample CSV file we will use for this tutorial:
First Name,Last Name,Email
Bobby,Penatac,bobbypenatac@company.com
Robert,Johnson,robertjohnson@company.com
Scott,Humphrey,scotthumphrey@company.com
Reading CSV file
In this section, you will learn how to read data from a CSV file.
const { parse } = require('csv-parse');
const fs = require('fs');
fs.createReadStream('./read-employees.csv')
.pipe(parse({ from_line: 2 }))
.on('data', function (row) {
console.log(row)
});
Now execute the script with the following command:
node readCSV.js
Output:
[ 'Bobby', 'Penatac', 'bobbypenatac@company.com' ]
[ 'Robert', 'Johnson', 'robertjohnson@company.com' ]
[ 'Scott', 'Humphrey', 'scotthumphrey@company.com' ]
Here the parse
method transforms the stream data into an array.
Writing CSV file
In this section you will learn how to write data into a CSV file.
First lets import the required packages:
const fs = require('fs');
const { stringify } = require("csv-stringify");
Lets add some dummy data to write into file:
...
const employees = [
{
firstName: 'Bobby',
lastName: 'Penatac',
email: 'bobbypenatac@company.com'
},
{
firstName: 'Robert',
lastName: 'Johnson',
email: 'robertjohnson@company.com'
},
{
firstName: 'Scott',
lastName: 'Humphrey',
email: 'scotthumphrey@company.com'
}
];
Next create a writeable stream for a while where you want to write the data:
...
const filename = "write-employees.csv";
const writableStream = fs.createWriteStream(filename);
Then define headers name for the CSV file:
...
const columns = [
'First Name',
'Last Name',
'Email'
];
At last add the following code to stringify and write data to target CSV file.
...
const stringifier = stringify({ header: true, columns: columns });
employees.forEach((employee) => {
stringifier.write(Object.values(employee));
});
stringifier.pipe(writableStream);
console.log('done');
Our full script looks like this:
const fs = require('fs');
const { stringify } = require("csv-stringify");
const employees = [
{
firstName: 'Bobby',
lastName: 'Penatac',
email: 'bobbypenatac@company.com'
},
{
firstName: 'Robert',
lastName: 'Johnson',
email: 'robertjohnson@company.com'
},
{
firstName: 'Scott',
lastName: 'Humphrey',
email: 'scotthumphrey@company.com'
}
];
const filename = "write-employees.csv";
const writableStream = fs.createWriteStream(filename);
const columns = [
'First Name',
'Last Name',
'Email'
];
const stringifier = stringify({ header: true, columns: columns });
employees.forEach((employee) => {
stringifier.write(Object.values(employee));
});
stringifier.pipe(writableStream);
console.log('done!');
Run you file in the terminal.
node writeCSV.js
You will see the following output:
done!
Now lets see content in the new file:
cat write-employees.csv
Output:
First Name,Last Name,Email
Bobby,Penatac,bobbypenatac@company.com
Robert,Johnson,robertjohnson@company.com
Scott,Humphrey,scotthumphrey@company.com
Conclusion
In this article, you learnt how to read and write CSV files using csv-parser
and csv-stringify
.