diff --git a/lib/internal/fs/recursive_watch.js b/lib/internal/fs/recursive_watch.js index 7d8b12eeb934..b16a1e3ed1cf 100644 --- a/lib/internal/fs/recursive_watch.js +++ b/lib/internal/fs/recursive_watch.js @@ -157,11 +157,20 @@ class FSWatcher extends EventEmitter { persistent: this.#options.persistent, }, (eventType, filename) => { const existingStat = this.#files.get(file); - const currentStats = statSync(file); + let currentStats; + try { + currentStats = statSync(file); + } catch (error) { + if (error.code === 'ENOENT') { + currentStats = false; + } else { + throw error; + } + } this.#files.set(file, currentStats); - if (currentStats.birthtimeMs === 0 && existingStat.birthtimeMs !== 0) { + if (!currentStats || (currentStats.birthtimeMs === 0 && existingStat.birthtimeMs !== 0)) { // The file is now deleted this.#files.delete(file); this.#watchers.delete(file);