How to append to a file in Node?

Tag: node.js Author: q83425398 Date: 2010-07-26

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', 'Hello Node', function (err) {
  if (err) throw err;
  console.log('It\'s saved!');
}); // => message.txt erased, contains only 'Hello Node'

Any idea how to do this the easy way?

ANSWER: found the answer myself:

var log = fs.createWriteStream('log.txt', {'flags': 'a'});
// use {'flags': 'a'} to append and {'flags': 'w'} to erase and write a new file
log.write("this is a message");


You can write your answer as an answer to your question and accept that answer. That way, things are more organized, and it's clear you've found your answer.

Best Answer

fs.appendFile('message.txt', 'data to append', function (err) {



Since Node v0.8.0
Does anyone know if fs.appendFile keeps a link to the file open so appends are faster? (rather than open/close each write)…

Other Answer1

You need to open it, then write to it.

var fs = require('fs'), str = 'string to append to file';'filepath', 'a', 666, function( e, id ) {
  fs.write( id, 'string to append to file', null, 'utf8', function(){
    fs.close(id, function(){
      console.log('file closed');

Here's a few links that will help explain the parameters


EDIT: This answer is no longer valid, look into the new fs.appendFile method for appending.


look like supercobra constantly writes log to the log file, usage of fs.write is not recommended in this case, use fs.createWriteStream instead. Read
The answer is no longer accurate as of nodejs v0.4.10.
@RubenTan why is that?
fs.write(fd, buffer, offset, length, position, [callback]) ...
it should be '0666' rather than 666.

Other Answer2

Your code using createWriteStream creates a file descriptor for every write. log.end is better because it asks node to close immediatelly after the write.

var log = fs.createWriteStream('log.txt', {'flags': 'a'});
// use {'flags': 'a'} to append and {'flags': 'w'} to erase and write a new file
log.end("this is a message");

Other Answer3

Node 0.8 has fs.appendFile:

fs.appendFile('message.txt', 'data to append', function (err) {
  if (err) throw err;
  console.log('The "data to append" was appended to file!');


Other Answer4

fd = fs.openSync(path.join(process.cwd(), 'log.txt'), 'a')
fs.writeSync(fd, 'contents to append')


anything sync() is almost always a bad idea unless you're 100% sure you absolutely NEED it. Even then, you're probably doing it wrong.
Doesn't mean it's wrong. It just does it Synchronously. Might not be best practice for Node.js, but it's supported.
I was using "ur doin it wrong" in the colloquial internet-meme sense of the phrase. Obviously it's supported =P
@LuisR. The whole point in using node is to do things asynchronously, allowing the processes to happen in the background while freeing the internal event loop to handle other requests (if it's a server) or other tasks.
Agreed on async, but sometimes if you're just writing an interactive script, sync is fine.