fs.watch
and fs.watchFile
can be used to watch file changes. However, there are some problems with them. Thus, we can find a lot of wrappers of these functions. Here I recommend Chokidar written by Paul Miller.
Installation
Install Chokidar via Node.js package manager:
npm install chokidar
Usage
Chokidar is very simple to use. Here is an example:
chokidar = require 'chokidar'
watcher = chokidar.watch 'files', persistent: true
watcher
.on 'add', (path) ->
console.log(path, 'added')
.on 'change', (path) ->
console.log(path, 'changed')
.on 'unlink', (path) ->
console.log(path, 'removed')
add
and change
also receive meta info as second argument:
watcher.on 'change' (path, stats) ->
console.log('File', path, 'changed size to', stats.size)
You can find the definition of stats
from http://nodejs.org/api/fs.html#fs_class_fs_stats.