在nodejs
fs模块。
const fs = require('fs');
fs.watch('target.txt', function() {
console.log("File 'target.txt' just changed!");
});
如果文件不存在,我得到:
fs.js:1172
throw errnoException(err, 'watch');
^
Error: watch ENOENT
at exports._errnoException (util.js:746:11)
at FSWatcher.start (fs.js:1172:11)
如果文件不存在,是否有一个 err
回调?这里处理错误的正确方法是什么?
最佳答案
您可以尝试像这样包装它,它会在执行任何操作之前检查文件是否存在。
if (fs.existsSync('target.txt')) {
fs.watch('target.txt', function() {
console.log("File 'target.txt' just changed!");
});
}
关于javascript - fs.watch 如果文件不存在,是否有错误处理程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36989315/