Node.js fs.watch() Method - GeeksforGeeks

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.
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.
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 moduleconst 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 namesetTimeout(  () => fs.renameSync("example_file.txt", "new_file.txt"),  1000);  // Renaming the file back to its old namesetTimeout(  () => 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 moduleconst 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 namesetTimeout(  () => 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
Node.js-fs-module
Node.js-Methods
Picked
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to install the previous version of node.js and npm ?
Difference between promise and async await in Node.js
How to use an ES6 import in Node.js?
Express.js res.sendFile() Function
Mongoose | findByIdAndUpdate() Function
Top 10 Front End Developer Skills That You Need in 2022
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Difference between var, let and const keywords in JavaScript