0
votes

When I use fs.watch on a directory: https://nodejs.org/docs/latest/api/fs.html#fs_fs_watch_filename_options_listener

it does not notice new files added to the dir after fs.watch is called..I am on Linux - is there some call I need to make for it to notice newly added / deleted files?

the only events on the returned object FSWatcher are "change", "error" and "close" - there is "add" or "delete" for files.

interface FSWatcher extends events.EventEmitter {
    close(): void;

    /**
     * events.EventEmitter
     *   1. change
     *   2. error
     */
    addListener(event: string, listener: (...args: any[]) => void): this;
    addListener(event: "change", listener: (eventType: string, filename: string | Buffer) => void): this;
    addListener(event: "error", listener: (error: Error) => void): this;
    addListener(event: "close", listener: () => void): this;

    on(event: string, listener: (...args: any[]) => void): this;
    on(event: "change", listener: (eventType: string, filename: string | Buffer) => void): this;
    on(event: "error", listener: (error: Error) => void): this;
    on(event: "close", listener: () => void): this;

    once(event: string, listener: (...args: any[]) => void): this;
    once(event: "change", listener: (eventType: string, filename: string | Buffer) => void): this;
    once(event: "error", listener: (error: Error) => void): this;
    once(event: "close", listener: () => void): this;

    prependListener(event: string, listener: (...args: any[]) => void): this;
    prependListener(event: "change", listener: (eventType: string, filename: string | Buffer) => void): this;
    prependListener(event: "error", listener: (error: Error) => void): this;
    prependListener(event: "close", listener: () => void): this;

    prependOnceListener(event: string, listener: (...args: any[]) => void): this;
    prependOnceListener(event: "change", listener: (eventType: string, filename: string | Buffer) => void): this;
    prependOnceListener(event: "error", listener: (error: Error) => void): this;
    prependOnceListener(event: "close", listener: () => void): this;
}
1

1 Answers

0
votes

Nevermind - fs.watch does see delete/add/create/unlinked files. My error was only passing file paths to fs.watch instead of passing directories. Better to just pass directories. To accomplishing something similar to fs.watch on Linux, you can use:

 inotifywait -m /path/one -m /path/two -m /path/three -e create \
           -e moved_to -e modify -e moved_from \
           -e move -e create -e delete -e delete_self