Open In App

How to monitor a file for modifications in Node.js ?

Last Updated : 22 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this post, we are going to explore how to monitor a file for modifications in Node.js. Sometimes, we might need to see any changes that are made into the specific file and execute some type of function on the basis of that file. To watch any changes that are made to the file system, we can use the fs module provided by node.js that provides a solution here. For monitoring a file of any modification, we can either use the fs.watch() or fs.watchFile() function.

The difference between the two is fs.watch() method can watch both files and the directories whereas the fs.watchFile is only designed for watching any changes in the file.

The fs.watch() is an inbuilt application programming interface method provided by the fs module. This regularly watches for any changes to the file in the given directory. This method returns a Watcher object that is basically used for tracking any changes in the file.

Syntax: 

fs.watch()

fs.watch( filename[, options][, listener] )

The fs.watchFile() method continuously watches for any changes in the given file. It contains a callback listener that always listens to the file and informs of any changes.

fs.watchFile()

fs.watchFile(filename[, options], listener)

Parameters: The above method accepts 3 parameters which are explained below:

  1. filename: This is a string, buffer, or a URL that represents the name of the file (Directory in fs.watch()) that is to be watched.
  2. options: This is a string or an object that is used for modifying the behavior of the method. This is an optional parameter. It further has the following parameters:
    1. persistent: This is a boolean value that specifies whether the process should continue or not while the file is being watched. The default value is true.
    2. recursive: This is a boolean value that specifies whether the subdirectories of the given directory should be watched or not. The default value is false.
    3. encoding: This is a string value that specifies the character encoding that needs to be used for passing the filename to the user.
  3. listener: This is a string value that specifies the character encoding that uses the filename which is passed to the listener.
    1. eventType: This is a string value that specifies the different types of modification on the file.
    2. filename: As the name suggests, this takes a string or buffer input for the filename.

The below examples illustrate the trigger event that is fired when any changes or modification is done upon the file in Node.js:

Example 1: This example shows any changes to the file with the help of fs.watch() method:

Javascript




// Node.js program to demonstrate the
// fs.watch() method
 
// Importing the filesystem module
const fs = require('fs');
 
fs.watch("example.txt", (eventType, filename) => {
  console.log("The file ", filename, " was modified!");
 
  // We can look for different types of changes on a file
  // using the event type like: rename, change, etc.
  console.log("It was a ", eventType, " event type.");
});
 
// Changing the contents of the file
setTimeout(
() => fs.writeFileSync("example.txt",
"Welcome to Geeks for Geeks, The file is modified"), 1000);


Output: 

The file example.txt was modified!

It was a change event type.

Note: The above method is unreliable and can display multiple events for every modification. that this method is unreliable and may show multiple events for every modification.

Example 2: This example shows any changes to the file with the help of fs.watchFile() method:

Javascript




// Node.js program to demonstrate
// the fs.watchFile() method
 
// Importing the filesystem module
const fs = require('fs');
 
fs.watchFile("example.txt", {
 
  // Passing the options parameter
  bigint: false,
  persistent: true,
  interval: 1000,
}, (curr, prev) => {
  console.log("\nThe file was edited");
 
  // Time when file was updated
  console.log("File was modified at: ", prev.mtime);
  console.log("File was again modified at: ", curr.mtime);
  console.log(
    "File Content Updated: ",
    fs.readFileSync("example.txt", "utf8")
  );
});
 
// Make Changes to the file for the first time
fs.writeFileSync("example.txt",
"Welcome to Geeks for Geeks");
 
// Make Changes to the file for the second time
setTimeout(
() => fs.writeFileSync("example.txt",
    "File is Edited Again!!!"),
3000
);


Output: 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads