Open In App

Node.js fs.promises.appendFile() Method

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.

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:




// 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.




// 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.




// 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.




// 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:


Article Tags :