diff --git a/lib/fs.js b/lib/fs.js index 58704e529747b2..e0a6ad32fea9ca 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -1278,7 +1278,7 @@ function StatWatcher() { this._handle.onchange = function(current, previous, newStatus) { if (oldStatus === -1 && - newStatus === -1 && + newStatus < 0 && current.nlink === previous.nlink) return; oldStatus = newStatus; diff --git a/test/parallel/test-fs-watch-file-nonexistent.js b/test/parallel/test-fs-watch-file-nonexistent.js new file mode 100644 index 00000000000000..cd45056c63fd57 --- /dev/null +++ b/test/parallel/test-fs-watch-file-nonexistent.js @@ -0,0 +1,13 @@ +'use strict'; + +var fs = require('fs'); + +var filename = '/path/to/file/that/does/not/exist'; + +fs.watchFile(filename, function() { + throw new Error('callback should not be called for non-existent files'); +}); + +setTimeout(function() { + fs.unwatchFile(filename); +}, 10);