Ajay
Ajay

Reputation: 189

Filesystem watcher using node.js

I want to implement file system watcher using node.js so that it watches a particular directory for any files being added/deleted. Can somebody give an idea how to implement this?

Thanks

Upvotes: 5

Views: 11866

Answers (5)

michal.jakubeczy
michal.jakubeczy

Reputation: 9459

Another alternative is to use NSFW. It is a native abstraction for Linux, Windows, and OSX file watching services, thus does not suffer from performance issues like others.

https://github.com/Axosoft/nsfw

Upvotes: 0

jinglesthula
jinglesthula

Reputation: 4577

As mentioned by @cbmeeks, the chokidar package is worth looking at. Its README mentions that it's used by webpack, browserify, VS Code, etc, so it's likely quite stable and painless to use. It has > 4k stars as well, so that lends some weight to it.

Upvotes: 2

balupton
balupton

Reputation: 48620

The core node.js watching will trigger a change event on the parent directory when a file is deleted, among other oddities. You can use Watchr to get useful events, and watching directory trees.

Upvotes: 5

Chris Simpson
Chris Simpson

Reputation: 7990

As pointed out in comments, the previous answer by @FailingBullets, whilst good to know, really only addresses individual file changes. There is a package here that might help though:

https://github.com/mikeal/watch

I haven't used it yet though I intend to soon. I'll update my answer once I do.

Upvotes: 2

fbstj
fbstj

Reputation: 1714

check out fs.watchFile(filename, [options], listener) for the inbuilt file system watching

Upvotes: 9

Related Questions