Skip to content
Related Articles

Related Articles

Node.js fs.watch() Method
  • Last Updated : 29 Jul, 2020

The fs.watch() method is an inbuilt application programming interface of fs module which is used to continuously watch for changes in the given file or directory. It returns a fs.FSWatcher object that can be used to track the changes in the file.

It has an optional listener as the third argument that can be used to get the changes that take place. This listener has two arguments, one is the name of the file or directory that is modified and the other is the type of modification. While watching a file, if it disappears and reappears, the ‘rename’ event is emitted, and if the file contents are modified the ‘change’ event is emitted.

Note: This method is unreliable and may show multiple events in the listener for each modification.

Syntax:

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

Parameters: This method accepts three parameters as mentioned above and described below:



  • filename: It is a string, Buffer, or URL that denotes the name of the file or directory to be watched.
  • options: It is a string or object that can be used to modify the behavior of the method. It is an optional parameter. It has the following parameters: 
    • persistent: It is a Boolean value that is used to specify if the process should continue as long as the files are being watched. The default value is true.
    • recursive: It is a Boolean value that is used to specify if all the subdirectories of the given directory should be watched. The default value is false.
    • encoding: It is a string that specifies the character encoding that has been used for the filename passed to the listener.
  • listener: It is a function that is invoked when the file is accessed or modified. It is an optional parameter.
    • eventType: It is a string that specifies the type of modification that the file underwent.
    • filename: It is a string or Buffer that specifies the filename on which the event is triggered.

Return Value: It returns a fs.StatWatcher object when the function is successfully called. 

Below examples illustrate the fs.watch() method in Node.js:

Example 1: This example shows the usage of the watch() method on a file.

javascript




// Node.js program to demonstrate the
// fs.watch() method
  
// Import the filesystem module
const fs = require('fs');
  
fs.watch("example_file.txt", (eventType, filename) => {
  console.log("\nThe file", filename, "was modified!");
  console.log("The type of change was:", eventType);
});
  
// Renaming the file to a new name
setTimeout(
  () => fs.renameSync("example_file.txt", "new_file.txt"),
  1000
);
  
// Renaming the file back to its old name
setTimeout(
  () => fs.renameSync("new_file.txt", "example_file.txt"),
  2000
);
  
// Changing the contents of the file 
setTimeout(
  () => fs.writeFileSync("example_file.txt"
  "The file is modified"), 3000
);

Output: Note that this method is unreliable and may show multiple events for every modification.

The file example_file.txt was modified!
The type of change was: rename

The file example_file.txt was modified!
The type of change was: rename

The file example_file.txt was modified!
The type of change was: change

Example 2: This example shows the usage of the watch() method on a directory.

javascript




// Node.js program to demonstrate the
// fs.watch() method
  
// Import the filesystem module
const fs = require('fs');
  
fs.watch("ex_dir", (eventType, filename) => {
  console.log("\nThe file", filename, "was modified!");
  console.log("The type of change was:", eventType);
});
  
// Changing the contents of a file 
setTimeout(
  () => fs.writeFileSync("ex_dir/ex1.txt"
  "The file is modified"), 1000
);
  
// Renaming a file to a new name
setTimeout(
  () => fs.renameSync("ex_dir/ex2.txt"
  "ex_dir/new_ex2.txt"), 2000
);

Output: Note that this method is unreliable and may show multiple events for every modification.

The file ex1.txt was modified!
The type of change was: change

The file ex2.txt was modified!
The type of change was: rename

The file new_ex2.txt was modified!
The type of change was: rename

Reference: https://nodejs.org/api/fs.html#fs_fs_watch_filename_options_listener

My Personal Notes arrow_drop_up
Recommended Articles
Page :