user394010
user394010

Reputation: 374

Difference in fs watch methods

What is the defference between methods of node.js file system watching:

(add more if any)

Upvotes: 3

Views: 2652

Answers (1)

Parker Tailor
Parker Tailor

Reputation: 1485

I was looking for info on this exact question and came across this post.

- Blog post in a nutshell:

fs.watch()

  • is a newer API and recommended.
  • uses native watching functions supported by OS, so doesn't waste CPU on waiting.
  • doesn't support all platforms such as AIX and Cygwin.

fs.watchFile()

  • is an old API and not recommended.
  • calls stat() periodically, so uses CPU even when nothing changes.
  • runs on any platform.

- Not in the blog post:

node-watch()

I haven't used node-watch myself, but, by looking at it, I can see it extends fs.watch() and adds recursive functionality. fs.watch() allows you to watch a directory for changes, but to watch all directories below would require separate calls. If I had to guess, (I've not tried it) these might be the same:

fs.watch(./project)

fs.watch(./project/assets)

fs.watch(./project/lib)

Or

node-watch(./project, { recursive: true })

Upvotes: 10

Related Questions