Package Exports
- node-watch
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (node-watch) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
node-watch 
A wrapper and enhancements for fs.watch.
Installation
npm install node-watchExample
var watch = require('node-watch');
watch('file_or_dir', { recursive: true }, function(evt, name) {
console.log('%s changed.', name);
});Now it's fast to watch deep directories on macOS and Windows, since the recursive option is natively supported except on Linux.
// watch the whole disk
watch('/', { recursive: true }, console.log);Why?
- Some editors will generate temporary files which will cause the callback function to be triggered multiple times.
- The callback function will only be triggered once on watching a single file.
Missing an option to watch a directory recursively.- Recursive watch is not supported on Linux or in older versions of nodejs.
- Keep it simple, stupid.
Options
The usage and options of node-watch are compatible with fs.watch.
persistent: Boolean(default true)recursive: Boolean(default false)encoding: String(default 'utf8')
Extra options
filter: RegExp | FunctionReturn that matches the filter expression.
// filter with regular expression watch('./', { filter: /\.json$/ }); // filter with custom function watch('./', { filter: f => !/node_modules/.test(f) });
Events
The events provided by the callback function is either update or remove, which is less confusing to fs.watch's rename or change.
watch('./', function(evt, name) {
if (evt == 'update') {
// on create or modify
}
if (evt == 'remove') {
// on delete
}
});Watcher object
The watch function returns a fs.FSWatcher like object as the same as fs.watch (>= v0.4.0).
var watcher = watch('./', { recursive: true });
watcher.on('change', function(evt, name) {
// callback
});
watcher.on('error', function(err) {
// handle error
});
// close
watcher.close();
// is closed?
watcher.isClosed()List of methods
.on.once.emit.close.listeners.setMaxListeners.getMaxListeners
Extra methods
.isCloseddetect if the watcher is closed
Known issues
Windows, node < v4.2.5
- Failed to detect
removeevent - Failed to get deleted filename or directory name
Misc
1. Watch multiple files or directories in one place
watch(['file1', 'file2'], console.log);2. Customize watch command line tool
#!/usr/bin/env node
// https://github.com/nodejs/node-v0.x-archive/issues/3211
require('epipebomb')();
var watcher = require('node-watch')(
process.argv[2] || './', { recursive: true }, console.log
);
process.on('SIGINT', watcher.close);Monitoring chrome from disk:
$ watch / | grep -i chromeAlternatives
License
MIT
Copyright (c) 2012-2017 yuanchuan
