Categories
JavaScript Answers

How to write to a CSV in Node.js?

Spread the love

To write to a CSV in Node.js, we call writeFile.

For instance, we write

let dataToWrite;
const fs = require("fs");

fs.writeFile("form-tracking/formList.csv", dataToWrite, "utf8", (err) => {
  if (err) {
    console.log("Some error occured");
  } else {
    console.log("It's saved!");
  }
});

to call writeFile to write the dataToWrite CSV string to the form-tracking/formList.csv file.

If there’re any errors then, err in the callback would be set.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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