Thursday, September 14, 2023
HomeNodejs3 Techniques to Add to a Data in Node.js (With Phrase Structure...

3 Techniques to Add to a Data in Node.js (With Phrase Structure & Instances)

In our file-handling trip, we experience several situations where we would certainly be called for to add info to data in Node.js. Whether it might be to upgrade a documents or log brand-new info onto a currently existing documents, adding is a must-know strategy. In this write-up, we’ll take a look at a couple of approaches through which we can add information to a documents, however initially, allow’s recognize just how documents dealing with operate in Node.js.

Data Handling in Node.js

Submit handling with Node.js entails procedures like analysis, composing, adding, as well as removing information from data. To take care of these procedures Node.js gives an integrated fs (documents system) component which can be set up utilizing npm as well as imported to our documents utilizing call for()

const fs = call for(' fs');.

Submit dealing with procedures can be executed synchronously, indicating just that a specific block of code is implemented each time, or asynchronously, significance code implementation is not obstructed when a specific block of code is implemented. Right here, we’ll concentrate on some integrated asynchronous as well as simultaneous features offered by the fs component for adding information to a documents.

Techniques for Appending Information to a Data in Node.js

Node.js gives numerous approaches for adding to a documents, several of them are as adheres to:

  • Utilizing fs.appendFile()
  • Utilizing fs.createWriteStream()
  • Utilizing fs.appendFileSync()

1. Utilizing fs.appendFile()

fs.appendFile() is an integrated feature in the fs component which assists us add information to a documents that currently exists. Its phrase structure is as adheres to.

Phrase Structure:

fs.appendFile( course, information, callback).


  • the course stands for the outright or family member course to the documents where we will certainly add information.
  • information is a string standing for the information that will certainly be added to the documents.
  • callback is an optional disagreement that calls a feature after the procedure is finished. It can be made use of to present a success message in the console if the procedure is finished efficiently or toss a mistake message if the procedure is not effective.


Below, we have actually a documents called file.txt which currently consists of some information.

File Before appendFile
Data prior to append procedure

Currently we will certainly add some information right into the documents utilizing the appendFile() approach

const fs = call for(' fs');.

const filePath=" file.txt";
const material=" nNew information to add to the documents utilizing appendFile()";.

fs.appendFile( filePath, material, (err) => > {
if (err) {
console.error(' Mistake:', err);.
} else {
console.log(' Information added efficiently.');.
} );.

Below, the filePath continuous is made use of to save the course to our documents ( file.txt) as a string. The material continuous shops the information we’ll add to the documents, we include ‘n’, a newline personality to add the brand-new information on the following line as well as out the exact same line as the material currently existing in the documents.

After that we call the appendFile() approach as well as come on the filePath as well as material as debates. We come on a callback feature that stands for a mistake things. This feature is called when the append procedure is finished as well as represents whether the procedure took place efficiently or really did not.


Data Appended Using appendFile
Information added to file.txt utilizing appendFile()
Success Message Displayed After Appending
Message showed on the incurable after information has actually been added

2. Utilizing fs.createWriteStream()

fs.createWriteStream() is an inbuilt feature that develops a writeable stream in a documents.

Phrase Structure:

fs.createWriteStream( course, choices).


  • course which is a string including the outright or family member course to the documents we will certainly be dealing with.
  • choices consist of the numerous choices that can be offered to the writeable stream. It normally holds strings or things. A few of the choices consist of flags, which are made use of to define choices like ‘ a’ for append


Allow’s add some information right into file.txt utilizing the createWriteStream() approach.

File Before createWriteStream
Submit prior to information has actually been added to it

createWriteStream() can be made use of with the open occasion, which opens up the documents as well as permits you to add and after that we can utilize it one more time by paying attention to the surface occasion, which shuts the documents as well as shows a message depending upon whether the append procedure achieved success.

const fs = call for(' fs');.

const material=" nNew information added to the documents utilizing createWriteSteam()!";.

const writeStream = fs.createWriteStream(' file.txt', {flags: 'a'} );.
writeStream.on(' open', () => > {
writeStream.write( material);.
} );.

writeStream.on(' surface', (err) => > {
if (err) {
console.error( err);.
console.log(' Information added efficiently!');.
} );.

Below, the ‘ a’ flag is made use of as an choice to suggest that the information kept in the material constant needs to be added. on() approach of the writeStream() pays attention to the open or surface occasion which opens up as well as shuts the documents ( file.txt) specifically. compose() creates the material onto the documents when the documents is opened up as well as end() suggests all the material has actually been composed.

When the surface occasion happens, the documents is shut as well as if the append achieved success it shows a success message on the incurable, if it’s not, a mistake is tossed.

Appended Using createWriteStream
Information added to file.txt utilizing createWriteStream()

3. Utilizing fs.appendFileSync()

This approach is a concurrent documents procedure that can be made use of to add information to a documents. It functions in a similar way to the appendFile() approach seen over other than for the reality that it is simultaneous while appendFile() is asynchronous. In this approach, a brand-new documents is produced if it does not currently exist.

Phrase Structure:

fs.appendFileSync( course, information, choices).


  • course stands for the absolute/relative course to the documents.
  • information stands for the material to be added to the documents.
  • choice consists of choices like flags like ‘ a’ for append, encoding which has the default worth ‘ utf8’, as well as settings which is an integer worth defining a documents setting.


File Before appendFileSync
Submit prior to information has actually been added to it

In this instance, we’ll utilize appendFileSync() in a try-catch block to add information to file.txt which shows a success message in the attempt obstruct if the information is added efficiently. The catch block tosses a mistake if information has actually not been added.

const fs = call for(' fs');.

const information=" nNew information to add to the documents utilizing appendFileSync()";.

attempt {
fs.appendFileSync(' file.txt', information);.
console.log(' Information added efficiently!');.
} catch (err) {
console.error( err);.


Data Appended Using appendFileSync
Information added to file.txt utilizing appendFileSync()


Adding information to a documents permits us to conveniently include as well as upgrade the components of a documents without overwriting its initial components. Right here we discovered 3 approaches offered by the fs component for adding information, fs.appendFile(), fs.createWriteStream(), as well as fs.appendFileSync() These approaches permit us to add information synchronously as well as asynchronously depending upon our demand. Understanding numerous append features will certainly aid us effortlessly deal with data.



Most Popular

Recent Comments