Categories
JavaScript Answers

How to write objects into file with Node.js?

Spread the love

To write objects into file with Node.js, we call the writeFileSync method.

For instance, we write

fs.writeFileSync("./data.json", JSON.stringify(obj, null, 2), "utf-8");

to call the writeFileSync method to write the stringified obj object to data.json.

We convert obj to a JSON string with JSON.stringify.

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 *