Welcome to a tutorial on how to add new rows to an existing CSV file in NodeJS. So you need to update a CSV file? Read on for the examples!
TABLE OF CONTENTS
DOWNLOAD & NOTES
Here is the download link to the example code, so you don’t have to copy-paste everything.
EXAMPLE CODE DOWNLOAD
Just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.
SORRY FOR THE ADS...
But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.
Buy Me A Coffee Code Boxx eBooks
NODEJS ADD ROWS TO CSV
All right, let us now get into the examples of adding new rows to an existing CSV file in NodeJS.
1) APPEND NEW ROWS
// (A) LOAD MODULES
// npm install csv-stringify
// https://www.npmjs.com/package/csv-stringify
const fs = require("fs"),
csvs = require("csv-stringify");
// (B) DATA TO APPEND
var data = [["A", "B"]];
// (C) CREATE CSV FILE
csvs.stringify(data, (err, output) => {
fs.appendFileSync("x-dummy.csv", output);
console.log("OK");
});
Appending new rows (to the end of the CSV file) should not be too much trouble:
- Load the required modules. We will use the file system
fs
andcsv-stringify
modules in this example. - Define the rows that you want to append in a nested array.
- For beginners who are lost –
csvs.stringify(DATA, CALLBACK)
will simply turnDATA
into a “CSV encoded string”.CALLBACK
is a function that will be called whenstringify()
is done “converting” theDATA
.- Very simply, we append the converted “CSV string” to the end of the CSV file.
2) PREPEND NEW ROWS
// (A) LOAD MODULES
// npm install csv-stringify csv-parser
// https://www.npmjs.com/package/csv-stringify
// https://www.npmjs.com/package/csv-parser
const fs = require("fs"),
csvp = require("csv-parser"),
csvs = require("csv-stringify");
// (B) READ CSV FILE
var rows = [];
fs.createReadStream("x-dummy.csv")
.pipe(csvp({ headers : false }))
.on("data", data => rows.push(data))
.on("end", () => {
// (C) REARRANGE TO FLAT ARRAY
rows.forEach((r,i) => rows[i] = Object.values(r));
// (D) PREPEND
rows.splice(0, 0, ["C", "D"]);
// (E) WRITE
csvs.stringify(rows, (err, output) => {
fs.writeFileSync("x-dummy.csv", output);
console.log("OK");
});
});
Appending new rows is easy, but sadly, prepend is a different story… There are no native NodeJS functions to prepend data to a file, thus a roundabout solution:
- Load the required modules – File system, CSV parser (CSV to array), and CSV stringify (array to CSV).
- Read the entire CSV file into a
rows
array. - Do some “data yoga” so that
rows
is in a “nice nested array” of[["COL", "COL"], ["COL", "COL"], ...]
- To prepend, simply add a new row to the top of
rows
. - Lastly, write the updated
rows
back into the CSV file.
3) INSERT NEW ROWS
// (A) LOAD MODULES
// npm install csv-stringify csv-parser
// https://www.npmjs.com/package/csv-stringify
// https://www.npmjs.com/package/csv-parser
const fs = require("fs"),
csvp = require("csv-parser"),
csvs = require("csv-stringify");
// (B) READ CSV FILE
var rows = [];
fs.createReadStream("x-dummy.csv")
.pipe(csvp({ headers : false }))
.on("data", data => rows.push(data))
.on("end", () => {
// (C) REARRANGE TO FLAT ARRAY
rows.forEach((r,i) => rows[i] = Object.values(r));
// (D) INSERT AT EXACT ROW
rows.splice(3, 0, ["E", "F"]);
// (E) SEARCH & INSERT AT ROW
let at = null;
for (let [i,r] of Object.entries(rows)) {
if (r.includes("Jou Doe")) { at = i; break; }
}
if (at !== null) { rows.splice(at, 0, ["G", "H"]); }
// (F) WRITE
csvs.stringify(rows, (err, output) => {
fs.writeFileSync("x-dummy.csv", output);
console.log("OK");
});
});
Finally, if you need to insert into “somewhere in the middle” – It’s pretty much the same as prepend.
- (A To C) Load the modules, and read the CSV file into an array.
- (D) Use the same old
ARRAY.splice(AT, REMOVE, [ROW])
to insert a new row. - (E) If you do not know where to insert, the only way is to search through all the rows to find the row number…
- (F) Save the updated CSV.
EXTRAS
That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.
PERFORMANCE NOTES
Take note that the above “prepend” and “insert” examples read the entire CSV file into the memory. While it works, it will take a lot of system resources to process massive CSV files. So do your own changes when it comes to dealing with such cases. There are quite a few options:
- Find ways to limit the number of rows in the CSV file.
- Change the process. Instead of feeding the entire file stream into CSV parse, read line by line instead.
- Similarly, instead of “stringifying” the entire file – Do it line by line, or by batch.
LINKS & REFERENCES
- Read CSV Files – Code Boxx
- Write CSV Files – Code Boxx
- CSV Stringify – NPM
- CSV Parse – NPM
THE END
Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!