Node.js fs.promises.appendFile() Method Last Updated : 08 Oct, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report The fs.promises.appendFile() method of File System module in Node.js is used to interact with the hard disk of the user's computer. The appendFile() method is used to append new data into the existing file or if the file does not exist then file is created first and after that given data is appended to it. The fs.promises.appendFile() method returns a resolved or rejected promise and hence avoid the callback nesting or callback hell problems that may occur in fs.appendFile() method. Syntax: fs.promises.appendFile( path, data, options ) Parameter: This method accepts three parameter path, data and options. Options is an optional parameter. path: It is a String, Buffer or URL that specifies the path to the target file in which given data is to be appended. data: It is a String or Buffer that is going to append to the target file. options: It is an optional parameter that affects the output in someway accordingly we provide it to the function call or not. encoding: It specifies the encoding technique, default value is 'UTF8'. mode: It specifies the file mode. File modes allow us to create, read, write, or modify a file. The default value is ‘0o666’. flag: It specifies the flag used while appending to the file. The default value is ‘a’. Return Value: It returns a resolved or rejected promise. The promise is resolved if data is successfully appended to the target file otherwise rejected with an error object if any error is occurred (example-specified file does not have write permission, etc.) Example 1: javascript // Importing File System module const fs = require('fs') // The readFile() method reads the file // and returns buffer form of the data fs.promises.readFile('./test.txt') .then(buff => { // File content before append const oldContent = buff.toString() console.log(`Before Append: ${oldContent}\n`) // Append operation return fs.promises.appendFile('./test.txt', '\nHey, I am newly added..!!') }) .then(() => { // Getting new file content return fs.promises.readFile('./test.txt') }) .then(buff => { // File content after append const newContent = buff.toString() console.log(`After Append: ${newContent}\n`) }) .catch(err => { console.log(err) }) we can implement the same functionality using async-await keywords. javascript // Importing File System module const fs = require('fs') const appendDataToFile = async (path, data) => { // The readFile() method reads the file // and returns buffer form of the data const oldBuffer = await fs.promises.readFile(path) // File content before append const oldContent = oldBuffer.toString() // Append operation await fs.promises.appendFile(path, data) const newBuffer = await fs.promises.readFile(path) // File content after append const newContent = newBuffer.toString() console.log(`Before Append: ${oldContent}\n`) console.log(`After Append: ${newContent}`) } appendDataToFile('./test.txt', '\nHey, I am newly added..!!') .catch(err => { console.log(err) }) Output: Example 2: When a given path to the filename does not exist. javascript // Importing File System module const fs = require('fs') // Append operation // If given file does not exist // it will be created first then // data is appended fs.promises.appendFile('./test.txt', 'Please add me to the test file..!!') .then(() => { // readFile() method reads the file // and returns buffer form of the data return fs.promises.readFile('./test.txt') }) .then(buff => { // Appended data const content = buff.toString() console.log(`Content : ${content}`) }) .catch(err => { console.log(err) }) Implementing the same functionality with async-await keywords. javascript // Importing File System module const fs = require('fs') const appendDataToFile = async (path, data) => { // Append operation // If given file does not exist // It will created first then // data is appended await fs.promises.appendFile(path, data) // readFile() method reads the file // and returns buffer form of the data const buff = await fs.promises.readFile(path) // File content after append const content = buff.toString() console.log(`Content : ${content}`) } appendDataToFile('./test.txt', 'Please add me to the test file..!!') .catch(err => { console.log(err) }) Directory structure before running the program: Directory structure after running the program: Output: Comment More infoAdvertise with us Next Article Node fs.existsSync() Method H hunter__js Follow Improve Article Tags : Web Technologies Node.js Node.js-fs-module Similar Reads Node JS fs.readFile() Method âIn Node.js, the fs.readFile() method is a fundamental tool for reading files asynchronously, allowing your application to remain responsive while accessing file data. This method is part of Node.js's File System (fs) module, which provides an API for interacting with the file system.Syntaxfs.readFi 3 min read Node.js fs.exists() Method The fs exists method in node is used to check if the input file or the directory exists or not. It is an inbuilt application programming interface of fs module which provides an API for interacting with the file system in a manner closely modeled around POSIX functions. Syntax:fs.exists( path, callb 2 min read Node fs.existsSync() Method In Node.js, the fs.existsSync() method checks if a file or folder exists at a given path. It's synchronous, meaning it pauses the program until it finds the result (either true if it exists, or false if it doesn't). Because it stops everything while it works, itâs best used for quick checks in small 3 min read Node fs.mkdir() Method The fs.mkdir() method in Node.js is used to create a directory asynchronously.Syntaxfs.mkdir(path, mode, callback)Parameters: This method accepts three parameters as mentioned above and described below: path: This parameter holds the path of the directory that has to be created.mode: This parameter 2 min read Node.js fs.truncate() Method The fs.truncate() method in node.js is used to change the size of the file i.e either increase or decrease the file size. This method changes the length of the file at the path by len bytes. If len represents a length shorter than the file's current length, the file is truncated to that length. If i 2 min read Node.js fs.renameSync() Method In Node.js, the fs.renameSync() method is part of the built-in File System (fs) module and is used to rename or move files and directories synchronously. This method is useful when you need to quickly change a file's name or move it to a different directory synchronously. It blocks the execution of 3 min read Node.js fs.rmdir() Method The fs.rmdir() method is used to delete a directory at the given path. It can also be used recursively to remove nested directories.Syntax: fs.rmdir( path, options, callback )Parameters: This method accepts three parameters as mentioned above and described below: path: It holds the path of the direc 3 min read Node.js fs.stat() Method The fs.stat() method is used to return information about the given file or directory. It returns an fs.Stat object which has several properties and methods to get details about the file or directory. Syntax:fs.stat( path, options, callback )Parameters: This method accept three parameters as mentione 3 min read Node.js fs.mkdtempSync() Method The fs.mkdtempSync() method is an inbuilt application programming interface of fs module which provides an API for interacting with the file system in a manner closely modeled around standard POSIX functions. The fs.mkdtempSync() method creates a unique temporary directory. This is the synchronous v 2 min read Node.js fs.realpath() Method The fs.realPath() method is used to compute the canonical pathname of the given path. It does so by resolving the ., .. and the symbolic links in the path. Syntax: fs.realpath( path, options, callback ) Parameters: This method accept three parameters as mentioned above and described below: path: It 3 min read Like