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