Categories
JavaScript Answers

How to write a line into a .txt file with Node.js?

Spread the love

To write a line into a .txt file with Node.js, we call the appendFile function.

For instance, we write

const fs = require("fs");

fs.appendFile("log.txt", "new data", (err) => {
  if (err) {
    // append failed
  } else {
    // done
  }
});

to call appendFile to append 'new data' to log.txt.

And we get the error from err if there’re any errors.

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 *