nodejs file append

How to Append File in NodeJS

NodeJS offers many powerful ways to work with files. Sometimes you may need to do simple things like appending new line to file in NodeJS. In this article, we will look at how to append file in NodeJS, synchronously, asynchronously and as a stream.

How to Append File in NodeJS

Let us assume that you have a text file at /home/ubuntu/data.txt. Here are the two ways to append new line to file in NodeJS. For both these methods, we will use fs node package which offers many useful methods to work with files.

1. Synchronous File Append

In this case we will use appendFileSync method to synchronously update files. Here you need to specify the full path to your file, and the text to be appended. Here is its syntax.

fs.appendFileSync( path, data )

Here is an example.

const fs = require('fs');
fs.appendFileSync('/home/ubuntu/data.txt', 'test new line');

Since the above method is synchronous, it will block the execution cycle till file append is complete. Synchronous file update is useful in case the subsequent lines of code depend on the latest information in your file.

2. Asynchronous File Append

In this cases, we use appendFile method to add new line of text asynchronously. Here is its syntax.

fs.appendFile( path, data[, options], callback )

Here is an example.

const fs = require('fs');
fs.appendFile('/home/ubuntu/data.txt', 'test new line', function (err) {
  if (err) throw err;
  console.log('New line added');

In case of asynchronous file append, the control will be returned back to the main execution thread, that is, subsequent lines of code will continue to be executed without waiting for the file append to be completed. This is useful if subsequent code does not depend on the latest information present in your file.

It is important to note that appendFile opens a new file handle every time you can call it. So it is useful if you want to update your file occassionally. However, you should avoid using it for frequent file updates such as log update, or call it in a loop.

In such cases, you must use fs.WriteStream() function as shown below.

3. Using WriteStream

WriteStream uses the same file handler to make multiple updates. It is a great way to continuously write to files, without running out of memory. Here is an example.

var stream = fs.createWriteStream("/home/ubuntu/data.txt", {flags:'a'});

[...Array(10000)].forEach( function (item,index) {
    stream.write(index + "\n");


In the above code we are simply looping from 1 to 10000 and appending the integers one by one to our file. In the first line, we create a stream. Next, we use it in a loop to write the loop index value in each iteration. Finally we close the stream with stream.end() function.

In this article, we have seen both synchronous and asynchronous ways to append file in NodeJS. We have also looked at how to append file as a stream.

Also read:

How to Run NodeJS App in Background
How to Fix NGINX Upstream Timed Out Error
How to Fix EADDRINUSE Error in NodeJS
How to Prevent Direct File Access in PHP
How to Enable ES6 Import in NodeJS

Leave a Reply

Your email address will not be published. Required fields are marked *