Node.js Append File

The appendFileSync() method is used to add new content to an existing file.

Example

const fs = require("fs");

fs.appendFileSync(
    "student.txt",
    "\nThis is new content."
);

console.log("Content appended successfully.");
        
Output:

Content appended successfully.

Explanation

  1. Import the fs module.
  2. Use appendFileSync().
  3. Give the file name.
  4. Give the new content.
  5. The new content is added at the end.
← Back to Home