Categories
JavaScript Answers

How to write an array to file with Node.js?

Spread the love

To write an array to file with Node.js, we call the writeFile method.

For instance, we write

require("fs").writeFile("./my.json", JSON.stringify(myArray), (err) => {
  if (err) {
    console.error("error");
  }
});

to call writeFile with the file path and a stringified version of the myArray array to write the content of myArray into the my.json file.

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 *