Outputting a CSV File from Node.js

Sharing is Caring

After reading this article, you will understand how to export data from a database or other system using a csv library in Node.js.

CSV stands for common separated values file which means that data can be stored in a tabular format with commas separating each value.

CSV files can open up in almost any spreadsheet program: Excel, Google Sheets, etc. The big difference between these files and others is that it’s not proprietary. The file format is intentionally simple: comma for each row, the end of a record is signified by a line break, and there’s only one sheet per file, you can’t sell cells, columns or rows and there’s no formulas.

For those that want to spend their time reading a specification, there’s a standard called RFC 4180.

Why use CSV?

CSV files are plain text files that are really easy to create in most backend systems. For the most part, CSV files can be imported into any database and be used in basically any other system.

CSV uses a very simple and fairly flat schema – basically there’s a single flat list of fields.

CSV is generally more readable than XML or JSON.

And, it’s honestly backwards compatible with basically everything. There’s rarely a case where you need to roll your own CSV parser or CSV writer.

Prerequisite

In this article, we will output a CSV file using a Node.js module called json2csv. We’ll be using json2csv because it’s a really easy library to use – it can simply take an array of objects and convert them to into a valid csv file.

In this article, I’ll be assuming you have some Basic JavaScript and ES6 experience.

Step 1: Install json2csv

At the terminal / command line run npm i “json2csv”

If you’re successful it should look something like this:

Step 2: Update Your Code to include json2csv and likely require the core filestream module (fs).

From json2csv we need to bring in the Parser and we’ll use fs to allow us to write to the filestream.

Step 3: Determine What Our Headers Need to Be in the CSV

The easiest way to determine what the headers should be is to probably look at one of the objects and pull the properties by using Object.keys()

Step 4: Parse the Object

Next we need to insatiate the Parser and parse the object. This is done really easily by simply calling new Parser and passing in our desired headers.

Step 5: Write to the FileStream

The filestream module is a core module that’s bundled into Node.js it has asynchronous methods or synchronous methods if you prefer. In the example today, I’m use the asynchronous method.

Wrapping It Up

json2csv is an excellent library for converting an object or array of objects into a well formed csv file without having to write a lot of boiler plate code.

Sharing is Caring

Brian is a software architect and technology leader living in Niagara Falls with 13+ years of development experience. He is passionate about automation, business process re-engineering, and building a better tomorrow.

Brian is a proud father of four: two boys, and two girls and has been happily married to Crystal for more than ten years. From time to time, Brian may post about his faith, his family, and definitely about technology.