Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference in fs watch methods

Tags:

node.js

watch

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

  • watch(filename,[, options],(filename) => {} ) (node-watch package)
  • fs.watch(filename[, options][, listener])
  • fs.watchFile(filename[, options], listener)

(add more if any)

like image 664
user394010 Avatar asked Sep 19 '25 22:09

user394010


1 Answers

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 })

like image 88
Parker Tailor Avatar answered Sep 21 '25 16:09

Parker Tailor