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\'
In addition to denysonique's answer, sometimes asynchronous type of appendFile
and other async methods in NodeJS are used where promise returns instead of callback passing. To do it you need to wrap the function with promisify
HOF or import async functions from promises namespace:
const { appendFile } = require('fs').promises;
await appendFile('path/to/file/to/append', dataToAppend, optionalOptions);
I hope it'll help