If you want to add multiple lines, you need to append the linefeed character:
var fs = require('fs');
var str = "";
for(var i=1; i<10; i++){
str += i + "\r\n"; // Linefeed \r\n usually on Windows and \n only on Linux
}
// Only one filewrite, to optimize
fs.writeFile('helloworld.txt', str, function (err) {
if (err) return console.log(err);
});
If you want to append it doing multiple filewrites because of reasons, you will also need the linefeed character.
var fs = require('fs');
for(var i=1; i<10; i++){
fs.appendFile('helloworld.txt', i + "\r\n", function (err) {
if (err) return console.log(err);
});
}
Protip: The key here is the linefeed character/s.
Protip 2: Keep your indentation clean.