fs.watchFile(filename[, options], listener)
Watch for changes on filename
. The callback listener
will be called each time the file is accessed.
The options
argument may be omitted. If provided, it should be an object. The options
object may contain a boolean named persistent
that indicates whether the process should continue to run as long as files are being watched. The options
object may specify an interval
property indicating how often the target should be polled in milliseconds. The default is { persistent: true, interval: 5007 }
.
The listener
gets two arguments the current stat object and the previous stat object:
fs.watchFile('message.text', (curr, prev) => { console.log(`the current mtime is: ${curr.mtime}`); console.log(`the previous mtime was: ${prev.mtime}`); });
These stat objects are instances of fs.Stat
.
If you want to be notified when the file was modified, not just accessed, you need to compare curr.mtime
and prev.mtime
.
Note: when an fs.watchFile
operation results in an ENOENT
error, it will invoke the listener once, with all the fields zeroed (or, for dates, the Unix Epoch). In Windows, blksize
and blocks
fields will be undefined
, instead of zero. If the file is created later on, the listener will be called again, with the latest stat objects. This is a change in functionality since v0.10.
Note: fs.watch()
is more efficient than fs.watchFile
and fs.unwatchFile
. fs.watch
should be used instead of fs.watchFile
and fs.unwatchFile
when possible.
Please login to continue.