How to use the watchFile function from fs
Find comprehensive JavaScript fs.watchFile code examples handpicked from public code repositorys.
fs.watchFile is a Node.js function that watches for changes to a specified file and calls a callback function when the file is modified.
135 136 137 138 139 140 141 142 143 144
resolve(ab) }) } function nocache(module, cb = () => { }) { fs.watchFile(require.resolve(module), async () => { await uncache(require.resolve(module)) cb(module) }) }
+ 11 other calls in file
31 32 33 34 35 36 37 38 39
* fs.symlink(): 新建文件的符号链接。 * fs.truncate(): 将传递的文件名标识的文件截 断为指定的长度。相关方法:fs.ftruncate()。 * fs.unlink(): 删除文件或符号链接。 * fs.unwatchFile(): 停止监视文件上的更改。 * fs.utimes(): 更改文件(通过传入的文件名指定)的时间戳。相关方法:fs.futimes()。 * fs.watchFile(): 开始监视文件上的更改。相关方法:fs.watch()。 * fs.writeFile(): 将数据写入文件。相关方法:fs.write()。 > 注意,上面fs提供的方法都是异步的,所谓异步的意思是,这些方法都提供了回调函数,方便异步触发相应的处理逻辑。
+ 5 other calls in file
How does fs.watchFile work?
The fs.watchFile method is a part of the Node.js file system module that watches for changes in the specified file or directory by polling the file at a set interval to check for changes in the file's size, timestamp, or other attributes. Once a change is detected, a change event is emitted.
GitHub: blockcollider/bcnode
93 94 95 96 97 98 99 100 101 102
exports.unlinkSync = fs.unlinkSync; exports.unwatchFile = fs.unwatchFile; exports.utimes = co.promisify(fs.utimes); exports.utimesSync = fs.utimesSync; exports.watch = fs.watch; exports.watchFile = fs.watchFile; exports.write = co.promisify(fs.write); exports.writeSync = fs.writeSync; exports.writeFile = co.promisify(fs.writeFile); exports.writeFileSync = fs.writeFileSync;
GitHub: minerdso/SKYREV
106 107 108 109 110 111 112 113 114 115
exports.startSock = startSock let file = require.resolve(__filename) fs.watchFile(file, () => { fs.unwatchFile(file) console.log(`Update '${__filename}'`) delete require.cache[file] require(file)
+ 8 other calls in file
Ai Example
1 2 3 4 5 6
const fs = require("fs"); fs.watchFile("test.txt", (curr, prev) => { console.log(`The current modification timestamp is: ${curr.mtime}`); console.log(`The previous modification timestamp was: ${prev.mtime}`); });
In this example, fs.watchFile is used to watch for changes to the file test.txt. When the file is modified, the callback function is called with two arguments: curr and prev, which represent the current and previous states of the file, respectively. The modification timestamps of the two states are logged to the console.
GitHub: Automattic/mongoose
190 191 192 193 194 195 196 197 198 199 200
// extra function to start watching for file-changes, without having to call this file directly with "watch" function startWatch() { files.forEach((file) => { const filepath = path.resolve(cwd, file); fs.watchFile(filepath, { interval: 1000 }, (cur, prev) => { if (cur.mtime > prev.mtime) { pugify(filepath, filemap[file]); } });
47 48 49 50 51 52 53 54 55 56 57 58
const watch = fs.watch(__filename, options, common.mustNotCall()); watch.close(); } { fs.watchFile(__filename, options, common.mustNotCall()); fs.unwatchFile(__filename); } {
fs.readFileSync is the most popular function in fs (2736 examples)