Sometimes, we want to write in a text file without overwriting with Node.js and JavaScript.
In this article, we’ll look at how to write in a text file without overwriting with Node.js and JavaScript.
How to write in a text file without overwriting with Node.js and JavaScript?
To write in a text file without overwriting with Node.js and JavaScript, we can use the appendFile
method.
For instance, we write:
const { promises: fs } = require("fs");
const write = async () => {
await fs.appendFile("file.txt", 'abc')
}
write()
to call fs.appendFile
with the path of the file to write to and the content to write into the file respectively.
The content will be added after the existing content of the file.
Conclusion
To write in a text file without overwriting with Node.js, we can use the appendFile
method.