Node.js fs.appendFile() Function - GeeksforGeeks

08 Oct, 2021
The fs.appendFile() method is used to asynchronously append the given data to a file. A new file is created if it does not exist. The options parameter can be used to modify the behavior of the operation.
Syntax:
fs.appendFile( path, data[, options], callback )
Parameters: This method accepts four parameters as mentioned above and described below:
path: It is a String, Buffer, URL or number that denotes the source filename or file descriptor that will be appended to.
data: It is a String or Buffer that denotes the data that has to be appended.
options: It is an string or an object that can be used to specify optional parameters that will affect the output. It has three optional parameters:encoding: It is a string which specifies the encoding of the file. The default value is ‘utf8’.mode: It is an integer which specifies the file mode. The default value is ‘0o666’.flag: It is a string which specifies the flag used while appending to the file. The default value is ‘a’.
encoding: It is a string which specifies the encoding of the file. The default value is ‘utf8’.
mode: It is an integer which specifies the file mode. The default value is ‘0o666’.
flag: It is a string which specifies the flag used while appending to the file. The default value is ‘a’.
callback: It is a function that would be called when the method is executed.err: It is an error that would be thrown if the method fails.
err: It is an error that would be thrown if the method fails.
Below examples illustrate the fs.appendFile() method in Node.js:
Example 1: This example shows the appending of the given text to a file.
// Node.js program to demonstrate the// fs.appendFile() method  // Import the filesystem moduleconst fs = require('fs');  // Get the file contents before the append operationconsole.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.
// Node.js program to demonstrate the// fs.appendFile() method  // Import the filesystem moduleconst fs = require('fs');  // Get the file contents before the append operationconsole.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

Reference: https://nodejs.org/api/fs.html#fs_fs_appendfile_path_data_options_callback
Node.js-fs-module
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to install the previous version of node.js and npm ?
Difference between promise and async await in Node.js
How to use an ES6 import in Node.js?
How to read and write Excel file in Node.js ?
Express.js res.render() Function
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Difference between var, let and const keywords in JavaScript