Open In App

Node fs.appendFile() Function

Last Updated : 11 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The fs.appendFile() method in Node.js is used to add data to the end of a file without deleting its existing content. It works asynchronously, which means it doesn’t block other operations. If the file doesn’t exist, it is automatically created. This method is useful for tasks like logging or saving updates.

Syntax:

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

Parameters:

This method accepts four parameters, as described below:

  1. path (String | Buffer | URL | Number):
    • This represents the file path or file descriptor where the data should be appended.
    • It can be a string specifying the file path, a Buffer containing the file path, a URL, or a file descriptor.
  2. data (String | Buffer):
    • The data that will be appended to the file.
    • This can be either a string or a Buffer containing the data to append.
  3. options (Optional) (String | Object):
    • An optional parameter that can either be a string or an object.
    • The object may include:
      • encoding (string): Specifies the file encoding. The default value is ‘utf8’.
      • mode (integer): Specifies the file permissions. The default value is 0o666 (read/write permissions).
      • flag (string): Specifies the file flag used when appending to the file. The default value is ‘a’ (append mode).
  4. callback (Function):
    • A callback function to execute after the operation is complete.
    • The err object will be passed as a parameter to the callback, indicating whether the operation was successful or if an error occurred.

Below examples illustrate the fs.appendFile() method in Node.js:

Example 1: This example shows the appending of the given text to a file.

JavaScript
// Node.js program to demonstrate the 
// fs.appendFile() method 

// Import the filesystem module 
const fs = require('fs');

// Get the file contents before the append operation 
console.log("\nFile Contents of file before append:",
    fs.readFileSync("example_file.txt", "utf8"));

fs.appendFile("example_file.txt", "World", (err) => {
    if (err) {
        console.log(err);
    }
    else {
        // Get the file contents after the append operation 
        console.log("\nFile Contents of file after append:",
            fs.readFileSync("example_file.txt", "utf8"));
    }
}); 

Output:

File Contents of file before append: Hello
File Contents of file after append: HelloWorld

Example 2: This example shows the usage of the optional parameters to change the file encoding, mode, and flag of the operation.

JavaScript
// Node.js program to demonstrate the 
// fs.appendFile() method 

// Import the filesystem module 
const fs = require('fs');

// Get the file contents before the append operation 
console.log("\nFile Contents of file before append:",
    fs.readFileSync("example_file.txt", "utf8"));

fs.appendFile("example_file.txt", " - GeeksForGeeks",
    { encoding: "latin1", mode: 0o666, flag: "a" },
    (err) => {
        if (err) {
            console.log(err);
        }
        else {
            // Get the file contents after the append operation 
            console.log("\nFile Contents of file after append:",
                fs.readFileSync("example_file.txt", "utf8"));
        }
    }); 

Output:

File Contents of file before append: This is a test file
File Contents of file after append: This is a test file - GeeksForGeeks

We have a Cheat Sheet on Node fs modules where we covered all the fs methods to check those please go through Node File System Complete Reference article.




Next Article

Similar Reads