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
.