删除监视文件夹时节点监视 EPERM

Node watch EPERM when the watched folder is deleted

本文关键字:监视 EPERM 节点 文件夹 删除      更新时间:2023-09-26

我很难在我的Windows机器上用nodejs fs.watch观看文件夹。删除监视文件夹时会引发异常。


    fs.watch('somedir', function (event, filename) {
        console.log('event is: ' + event);
        if (filename) {
            console.log('filename provided: ' + filename);
        } else {
            console.log('filename not provided');
        }
    });

当我删除某人时,它会引发异常而不是回调。

fs.watch 返回一个 fs。FSWatcher 对象,这可能会引发错误。

我刚刚对此进行了测试,删除文件夹时似乎会引发错误事件。以下是处理它的代码:

var fs = require('fs');
var path = "C:''somedir";
var watcher = fs.watch(path, function (event, filename) {
  console.log('event is: ' + event);
  if (filename) {
    console.log('filename provided: ' + filename);
  } else {
    console.log('filename not provided');
  }
});

watcher.on('error', function(err) {
  if (!fs.existsSync(path)) {
    console.log('folder deleted');
  }
});