How can I append to a file using fs.write()?
Using fs.write on the same files overwrites the content:
var fs = require('fs');
try {
fs.write("file.txt", "Hello World", 'w');
fs.write("file.txt", "Hello World", 'w');
} catch(e) {
console.log(e);
}
Use append mode a instead of [over]write mode w in the fs.write call.
var fs = require('fs');
try {
fs.write("file.txt", "Hello World", 'a');
fs.write("file.txt", "Hello World", 'a');
} catch(e) {
console.log(e);
}
I inferred this based on the python C open()fopen documentation; Glad it worked, other file modes may work but were not tested by me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With