Nodejs Monitor File Changes or modifications Tutorial

There is a method in Nodejs API for FS module, which can be used to Monitor file changes of Modifications, which we will discuss with example step by step in this tutorial.Consider you need to monitor a file for changes and Number of new entries has been added to this file and you need need to monitor this file.For doing this we can use and fs.watchFile(). So, Let’s learn In this How-To Tutorial:  Nodejs Monitor File Changes.

The methods we can use to watch files in Node’s FS API are:

Article Contents, options, listener)

  • It will check for changes in a file and it will return an object fs.FSWatcher  , where filename is a file o directory.
  • In the options, you have three options persistent, recursive, and encoding.The persistence is used to define if the file is on watch or not and the default value is truerecursive is used to define if the current directory or all sub-directories to be watched for changes or modifications and the default value is false.encoding is used to Specify the character encoding to be used for the filename passed to the listener and the default value is utf8.
  • listener is a callback function with two arguments, (eventType, filename), where eventType can be rename or change and filename is the file on the watch.
  • options are optional.

Let’s see with an example.

How to monitor a file for modifications in Nodejs using with example


const fs = require('fs');

const filePath = 'Drive:\\path\to\file\users.txt';

const file = fs.readFileSync(filePath);

console.log('Initial File content: ' + file);, function(eventName, filename) {
    console.log('Event : ' + eventName);
    console.log(filename + ' file Changed ...');
    file = fs.readFileSync(filePath);
    console.log('File content at : ' + new Date() + ' is \n' + file);
    console.log('filename not provided or check file access permissions')

So, at start user file will only contain one name and it will be displayed in the console.Now we will add the another name and save the file and we can see that the new file will be shown in the console and date with time will also be displayed in the console. As you can see in the below gif.

See also  How To Exit from NodeJS Console From Script and Command

FS Watch Example


Now, we will learn, how to use fs.watchFile:

fs.watchFile(filename, options, listener)

  • The filename is the file you want to monitor for change or modifications.
  • The options had two options persistent and interval. The persistent indicates whether the process should continue to run as long as files are being watched and the default value is true and The interval indicates how often the file should be checked for changes in milliseconds and the default value is 5007.
  • The options should object and are optional. The default object will be  { persistent: true, interval: 5007 }
  • The listener is a callback function and will be called each time function is called.It has two arguments (curr, prev), where the curr is the current status object and the prev is the previous status object.

Let’s see with an example:

Nodejs Monitor File Changes using fs.watchFile with example


const fs = require('fs');
const filePath = './users.txt';

var file = fs.readFileSync(filePath);

console.log('Initial File content : ' + file);

fs.watchFile(filePath, { persistent: true, interval: 100 }, function() {
    console.log('File Changed ...');
    file = fs.readFileSync(filePath);
    console.log('File content at : ' + new Date() + ' is \n' + file);


again, we will use the users.txt file to test this method and result is as shown in below gif

FS watchFile Example

So, folks, that’s all, if this tutorial helped, please share with fellow developers and Subscribe to our Newsletter for awesome freebies.

Leave a Comment