To use the appendFileSync()
function from the fs-extra
package in Node.js, follow these steps:
fs-extra
package by executing this command in your project directory:index.tsx21 chars2 lines
fs
and fs-extra
modules in your JavaScript file:index.tsx63 chars3 lines
appendFileSync()
function to append data to a file synchronously:index.tsx71 chars3 lines
This code appends the data
string to a file named file.txt
in the current working directory. If the file does not exist, it will be created. The function appendFileSync()
is synchronous, so it will block execution until the data is written to the file.
Alternatively, if you only need to append a small amount of data, you can use the fs.appendFileSync()
method that is built into Node.js:
index.tsx66 chars3 lines
This code does the same thing as the previous example, but with the native fs
module instead of fs-extra
.
gistlibby LogSnag