Node.js | fs.unwatchFile() Method
The fs.unwatchFile() method is used to stop watching for changes on the given file. An optional listener parameter can be specified to remove only the specified listener from the file. Otherwise, all the listeners associated with the file are removed. If the file is not being watched when this function is used, then it does not perform any operation and throw any error.
Parameters: This method accepts two parameters as mentioned above and described below:
- filename: It is a String, Buffer or URL that denotes the file that has to be stopped from watching.
- listener: It is a function that specifies the listener previously attached using the fs.watchFile() function. If specified, only this particular listener is removed. It is an optional parameter.
Below examples illustrate the fs.unwatchFile() method in Node.js.
The file was edited Previous Modified Time: 2020-05-30T08:43:28.216Z Current Modified Time: 2020-05-30T08:43:37.208Z File has been stopped watching
Listener 1: File Modified Listener 2: File Modified Listener 1 has been stopped! Listener 2: File Modified