Node watch EPERM when the viewed folder is deleted

It’s hard for me to watch the folder with the nodejs fs.watch node on my windows machine. It throws an exception when the deleted folder is deleted.

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

When I remove somedir, it throws an exception, not a callback.

+4
source share
1 answer

fs.watch returns fs.FSWatcher , which may cause an error.

I just tested this, and it seems that an error event occurs when a folder is deleted. Here is the code to handle it:

 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'); } }); 
0
source

Source: https://habr.com/ru/post/1500711/


All Articles