How to Append Data to a File in Node.js

In NodeJS, to append data to a a file, you can use a quick and simple method in the “fs” module.

How to Append Data to a File in Node.js

//load the fs module
var fs = require('fs');

fs.appendFile('/path/file.txt', "\nAnother Line.", function (err) {
  if(!err) {
    console.log("file successfully saved.");
  } else {
    console.log("error", err);
  }
});