How to append in a text file in Node.js?
This blog describes how to append in a text file in Node.js.
To append to a text file in Node.js, you can use the fs.appendFile() function from the built-in fs (File System) module. This function appends data to a file, creating the file if it does not exist. Here’s how you can use it.
const fs = require('fs');
// Path to the file
const filePath = 'source.txt';
// Data to append
const dataToAppend = 'New data to append to the file.\n';
// Append data to the file
fs.appendFile(filePath, dataToAppend, (err) => {
if (err) {
console.error('Error appending to file:', err);
return;
}
console.log('Data appended to file successfully.');
});
The fs.appendFile() function takes three arguments.
- The path to the file.
- The data to append.
- A callback function that gets called after the operation completes. It receives an error object if an error occurred during the operation.
This code will append the specified data to the end of the file. If the file does not exist, it will be created.
source.txt

Output


Further Reading
Spring Framework Practice Problems and Their Solutions
From Google to the World: The Story of Go Programming Language
Why Go? Understanding the Advantages of this Emerging Language
Creating and Executing Simple Programs in Go
20+ Interview Questions on Go Programming Language
100+ MCQs On Java Architecture
- Angular
- ASP.NET
- C
- C#
- C++
- CSS
- Dot Net Framework
- HTML
- IoT
- Java
- JavaScript
- Kotlin
- PHP
- Power Bi
- Python
- Scratch 3.0
- TypeScript
- VB.NET

Excellernt article. Keeep writing sch kind of inforrmation oon yourr site.
Im really impressed by it.
Heyy there, Youu have performed a fantastic job. I’ll certainly dig it annd inn my view
suggestt to mmy friends. I am confident tey
will be benefited from this web site. xnxx
Thiss website certainly has all the info I needed about this subjdct andd
didn’t knoww who too ask. xnxx
Thanks for a marvelous posting! I seriously enjoyed reading it, you’re a great author.I wilkl
ensure that I bookmark your blog and will evenntually
come back from now on. I want tto encourage you continue your great work, have
a nioce morning! xnxx