I am trying to append a string to a log file. However writeFile will erase the content each time before writing the string.
fs.writeFile(\'log.txt\'
Use a+ flag to append and create a file (if doesn't exist):
a+
fs.writeFile('log.txt', 'Hello Node', { flag: "a+" }, (err) => { if (err) throw err; console.log('The file is created if not existing!!'); });
Docs: https://nodejs.org/api/fs.html#fs_file_system_flags