How to append to a file in Node.js?

Introduction:

In this blog post, We will learn how to append contents to the end of the file instead of overwriting the file’s content. Sometimes it is necessary to keep the previous content of a file for debugging purposes. For example, if you are logging into a file then in this case you don’t want to overwrite its content.  Node.js core API provides methods to make it easier for us. Node.js fs module provides API to do file operations.

To append data to file in Node.js, use fs.appendFile() function  to asynchronously append content to a file and fs.appendFileSync() function to synchronously append the data to a file.

fs.appendFile()

Syntax:

The syntax of appendFile() function is:

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

This method accept four parameters, described as follows:

  • path: The path of the file source file in which data will be appended. It could be a string, buffer or URL.
  • data:  It is a String or Buffer that denotes the data that has to be appended.
  • options: It is an optional arguments to specify encoding/mode/flag
  • callback: It is a callback function that is called after append to the file. It takes two parameters:
    • err: If any error occurs.

Below code snippet illustrates usage of fs.appendFile() function in Node.js:

// Example Node.js code to append data to file
let fs = require('fs');
 
const data = "Append data to a file asynchronously in Node.js";
 
// append data to file
fs.appendFile('sample.txt',data, 'utf8',
    // callback function
    function(err) {     
        if (err) throw err;
        console.log("Data has been appended to file successfully.")
});

Output:

Topcode@Topcode-MacBook-Pro $ node nodejs-append-to-file-async.js
Data has been appended to file successfully.

fs.appendFileSync()

Syntax:

The syntax of appendFileSync() function is:

fs.appendFileSync(path, data, options);

This method accept three parameters, described as follows:

  • path: The path of the file source file in which data will be appended. It could be a string, buffer or URL.
  • data:  It is a String or Buffer that denotes the data that has to be appended.
  • options: It is an optional arguments to specify encoding/mode/flag.

Below code snippet illustrates usage of fs.appendFileSync() function in Node.js:

// Example Node.js code to append data to file
let fs = require('fs');
 
const data = "Append data to a file synchronously in Node.js";
 
// append data to file
fs.appendFileSync('sample.txt',data, 'utf8');
console.log("Data has been appended to file successfully.")

Output:

Topcode@Topcode-MacBook-Pro $ node nodejs-append-to-file-sync.js
Data has been appended to file successfully.

Conclusion:

Here we have learnt how we can use fs.appendFile() and fs.appendFileSync() to append data to a file in Node.js. If you are writing code for production use then it is advisable to use asynchronous API instead of synchronous API as synchronous function blocks the event loop of Node.js.

If you want to understand how to write to file instead of append then you can use fs.writeFile() function. You get more information from here

Reference:

FS Module

#HappyCoding #TopCode

#append data to file node js #append data to json file nodejs #node js append data to json file #append data to file nodejs #append to file in node js #how to append data in file in node js #how to append data in json file using node js #append data to file in node js #add data to json file node js append function node.js #insert function in node js #append nodejs #how to append in node js #fs.appendFIle() in node.js #fs.appendFileSync() in node.js fs appendfile function #fs readdir function #fs.appendfile is not a function #fs.appendfile callback must be a function #fs.appendfile async #fs.appendfile not working #fs.appendfile nodejs #file append funtion in nodejs #node.js append to file #nodejs append function #node.js append to file example #append file example in nodejs

, , , , ,

Related posts

Latest posts

Leave a Comment

Leave a Reply

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

Please disable your adblocker or whitelist this site!

How to whitelist website on AdBlocker?

How to whitelist website on AdBlocker?

  1. 1 Click on the AdBlock Plus icon on the top right corner of your browser
  2. 2 Click on "Enabled on this site" from the AdBlock Plus option
  3. 3 Refresh the page and start browsing the site