Why fs.watchFile called twice in Node?

node.js

Solution

fs.watchFile is unstable. From the node docs:

fs.watchFile(filename, [options], listener)#

Stability: 2 - Unstable. Use fs.watch instead, if available.

You can try `fs.watch`, but unfortunately it may suffer from the same problem. I had the same issue with `fs.watch` on windows, when trying to create a similar monitor script.

The workaround was to log the time when the modification occurs and ignore the second change if it was triggered withing a few milliseconds. A bit ugly but it worked.

Problem

`Ubuntu 12.04` `Node v0.6.14` `CoffeeScript 1.3.1` ``` fs.watchFile coffee_eval, (e) -> console.log e result = spawn 'coffee', ['-bc', coffee_eval] msg = '' result.stderr.on 'data', (str) -> msg+= str result.stderr.on 'end', -> console.log 'msg: ', msg print "!! #{coffee_eval}\n" ``` Whole code on gist: https://gist.github.com/2621576 Every time I save a file which is watched, the main function is called twitce rather than once. My Editor is Sumlime Text 2. the output words can be see :

Original source

Related problems