How to use the watchFile function from fs-extra

Find comprehensive JavaScript fs-extra.watchFile code examples handpicked from public code repositorys.

fs-extra.watchFile is a method that watches a file for changes and executes a callback function when the file is modified.

245
246
247
248
249
250
251
252
253
- fs.symlink():创建指向文件的新符号链接
- fs.truncate():将传递的文件名标识的文件截断为指定的长度。有关:fs.ftruncate()
- fs.unlink():删除文件或符号链接
- fs.unwatchFile():停止监视文件上的更改
- fs.utimes():更改通过传递的文件名标识的文件的时间戳。有关:fs.futimes()
- fs.watchFile():开始监视文件上的更改。有关:fs.watch()
- fs.writeFile():将数据写入文件。有关:fs.write()

当然这些api默认都是异步的,如果想要使用同步方式只需在方法名后追加Sync即可,而且同步方式需要使用try..catch方式捕获错误,还会阻塞主线程执行
fork icon0
star icon12
watch icon1

+ 13 other calls in file

74
75
76
77
78
79
80
81
82
  pth = path.resolve(path.dirname(this.options.filename), pth)
  return require(pth)
}

watch(child, parent) {
  fs.watchFile(child, { interval: 300 }, (curr, prev) => {
    if (curr.mtime > prev.mtime) {
      fs.open(parent, 0, function(err, fd) {
        if (err) return console.log(err)
fork icon1
star icon4
watch icon0

How does fs-extra.watchFile work?

fs-extra.watchFile is a method provided by the fs-extra module in Node.js that watches for changes in a file or directory and invokes a callback function whenever the file or directory changes. It works by periodically polling the file or directory to check if its modification time or size has changed and triggering the callback if a change is detected.

2485
2486
2487
2488
2489
2490
2491
2492
2493
2494

//=============================[to get message of New Update of this file.]===================================================

let file = require.resolve(__filename)

fs.watchFile(file, () => {

    fs.unwatchFile(file)

    console.log(`Update ${__filename}`)
fork icon19
star icon1
watch icon1

+ 19 other calls in file

14
15
16
17
18
19
20
21
22
23
24
var lesname = ""
for (let i = 0; i < files.length; i++) {
 //lesname += readall(files[i])
}


/*let file = fs.watchFile(path.join(appDir,"reports"), (curr, prev) => {
  console.log(`Current modified time: ${curr.mtime}`);
  console.dir(curr);
  console.log(`Previous modified time: ${prev.mtime}`);
});*/
fork icon0
star icon0
watch icon1

Ai Example

1
2
3
4
5
6
const fs = require("fs-extra");

fs.watchFile("/path/to/file", (curr, prev) => {
  console.log("The current modification timestamp is: ", curr.mtime);
  console.log("The previous modification timestamp was: ", prev.mtime);
});

This will watch the file at /path/to/file for changes and log the modification timestamps when the file is modified.

123
124
125
126
127
128
129
130
131
132
}
const source = fs.realpathSync(path.join(jlab.linkedPackages[name], rest));
if (source === fs.realpathSync(localPath)) {
  return;
}
fs.watchFile(source, { interval: 500 }, function (curr) {
  if (!curr || curr.nlink === 0) {
    return;
  }
  try {
fork icon0
star icon0
watch icon1

+ 17 other calls in file

44
45
46
47
48
49
50
51
52
53
54


exports.watchFile =
  /*#__PURE__*/
  (function() {
    var _ref = _asyncToGenerator(function*(filename, callback) {
      fs.watchFile(filename, () => {
        callback();
      });
      yield callback();
    });
fork icon0
star icon0
watch icon1

+ 9 other calls in file

184
185
186
187
188
189
190
191
192
193
  }
  return;
}

tsxFiles.forEach((filePath) => {
  fse.watchFile(filePath, { interval: 500 }, async () => {
    if ((await transpileFile(filePath, program, true)) === 0) {
      console.log('Success - %s', filePath);
    }
  });
fork icon0
star icon0
watch icon1

+ 3 other calls in file

function icon

fs-extra.readFileSync is the most popular function in fs-extra (9724 examples)