To write to a JSON file in JavaScript using Node.js, you can use the fs
module which is built into Node.js. Here's an example code snippet:
index.tsx403 chars17 lines
In this example, we start by requiring the fs
module. We then create a JavaScript object called myObj
. We convert this object to a JSON string using JSON.stringify()
. Finally, we use fs.writeFile()
to write the JSON string to a file called myFile.json
.
Note: If the file myFile.json
doesn't exist, fs.writeFile()
will create it. If it already exists, fs.writeFile()
will overwrite it with the new JSON data.
gistlibby LogSnag