Categories
JavaScript Answers Nodejs

How to write in a text file without overwriting with Node.js and JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *