append new content to existing file in node js code example
Example 1: Append text into a file nodejs
const fs = require('fs');
fs.appendFile('message.txt', 'data to append', function (err) {
if (err) throw err;
console.log('Saved!');
});
If message.txt doesnt exist, It will gonna create that too
Example 2: Append text into a file nodejs
Synchronously
const fs = require('fs');
fs.appendFileSync('message.txt', 'data to append');