How to read a large CSV file in Node.js

Today, we will show you how to read a large CSV file in Node.js. In this article, we will create an example to read a large CSV file using csv-parse.

Here, we will use the previously generated large CSV file. Refer the following article for more details.
How to write a large CSV file in Node.js

Solution

Use the following code to read the large CSV file using Node.js. Here we have used the fs.createReadStream along with the csv-parse npm package to read a CSV file.

In the above code, we are trying to read product CSV file that contains the product name and product SKU. You can process the data immediately after reading.

API Example

Your server.js file should look like below if you add this logic to the rest API. In this code we read the CSV file from the root level.

Refer this article to setup REST API in node.js.
Create REST API in Node.js

server.js

Here, we have logged the row data on data listener. Also, we returned success: true message at the end of the process and added the listener to catch the error.

Run the following command to read the CSV.

That’s it for today.
Thank you for reading. Happy Coding..!! 🙂

Demo & Source Code

GitHub Repository
If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

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