To use the outputJsonSync
function from the fs-extra
package in Node.js, you first need to install the package using npm:
index.tsx21 chars2 lines
Then, you can use the function to write a JSON file synchronously (blocking) like this:
index.tsx254 chars15 lines
In the code snippet above, we are first importing the fs-extra
module and assigning it to the variable fs
. Then, we are creating a jsonObj
object which we want to write to a JSON file.
We are calling the outputJsonSync
function of fs
using the path to the file we want to write to and the jsonObj
object we want to write. Since the function is synchronous, it blocks the execution of the program until the file has been written successfully.
Finally, we are logging a success message to the console if the write was successful, or an error message if it failed.
gistlibby LogSnag