0

I try to write some nodejs code that completes an existing file. But each time I start the script, it over writes the content of my test file.

var fs = require('fs');
var writer = fs.createWriteStream('test.txt', {flags: 'w'});
writer.on('finish', function() {
    console.log('data has been saved successfully');
});

function writeInList(id) {
  console.log(id); 
  writer.write(id+' \n');
}

for (var id = 0; id<10; id++){
  writeInList(id);
}

writer.end();

Of course I have searched for a solution:

Writing large files with Node.js

But I'm not able to make it run. Could anybody help me please?

Community
  • 1
  • 1
NewbieXXL
  • 155
  • 1
  • 1
  • 11
  • 1
    set your stream option flags to 'a' (append) – Sebastien Daniel Jun 17 '16 at 12:04
  • thanks. Where can I find an overview about all options. https://nodejs.org/api/fs.html#fs_fs_createwritestream_path_options is not helpful for me. – NewbieXXL Jun 17 '16 at 12:07
  • Only reason I know is that I had the exact same issue yesterday. I've yet to find a list of possible flags values for stream options... have been unable to locate that info in the NodeJS docs. – Sebastien Daniel Jun 17 '16 at 12:12

0 Answers0