What is node-watch?
The node-watch package is a simple and efficient file system watcher for Node.js. It allows you to monitor changes in files and directories, making it useful for tasks such as live-reloading, automated testing, and more.
What are node-watch's main functionalities?
Watch a single file
This feature allows you to watch a single file for changes. The callback function is triggered whenever the file is modified.
const watch = require('node-watch');
watch('file.txt', { recursive: false }, function(evt, name) {
console.log('%s changed.', name);
});
Watch a directory recursively
This feature allows you to watch a directory and all its subdirectories for changes. The callback function is triggered whenever any file within the directory or its subdirectories is modified.
const watch = require('node-watch');
watch('directory', { recursive: true }, function(evt, name) {
console.log('%s changed.', name);
});
Filter files to watch
This feature allows you to specify a filter to watch only certain types of files. In this example, only JavaScript files within the directory are monitored for changes.
const watch = require('node-watch');
watch('directory', { filter: /\.js$/ }, function(evt, name) {
console.log('%s changed.', name);
});
Other packages similar to node-watch
chokidar
Chokidar is a highly efficient and feature-rich file system watcher for Node.js. It supports recursive watching, filtering, and more advanced features like debouncing and throttling. Compared to node-watch, Chokidar offers more configuration options and better performance for large-scale projects.
gaze
Gaze is another file watcher for Node.js that supports recursive watching and filtering. It is known for its simplicity and ease of use. While it offers similar functionalities to node-watch, it may not be as performant or feature-rich as Chokidar.
watch
The watch package is a simple file watcher for Node.js. It provides basic functionality for monitoring file changes but lacks some of the advanced features found in Chokidar and node-watch. It is suitable for smaller projects or simpler use cases.
node-watch
A wrapper and enhancements for fs.watch (with 0 dependencies).
Installation
npm install node-watch
Example
var watch = require('node-watch');
watch('file_or_dir', { recursive: true }, function(evt, name) {
console.log('%s changed.', name);
});
This is a completely rewritten version, much faster and in a more memory-efficient way.
So with recent nodejs under OS X or Windows you can do something like this:
watch('/', { recursive: true }, console.log);
Why?
- Some editors will generate temporary files which will cause the callback function to be triggered multiple times.
- When watching a single file the callback function will only be triggered once.
Missing an option to watch a directory recursively.- Recursive watch is not supported on Linux or in older versions of nodejs.
Changelog
- The
filter
option can be of either Function or RegExp type since v0.5.3. - The
recursive
option is default to be false
since v0.5.0. - The callback function will always provide an event name since v0.5.0.
- Returns a fs.FSWatcher like object since v0.4.0.
Events
The events provided by the callback function would be either update
or remove
.
watch('./', function(evt, name) {
if (evt == 'remove') {
}
if (evt == 'update') {
}
});
Watcher object
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) {
});
watcher.on('error', function(err) {
});
watcher.close();
filter <RegExp>
as a regular expression for filtering with easefilter <Function>
as a function to filter files
watch('./', { filter: /\.json$/ }, console.log);
watch('./', {
recursive: true,
filter: function(name) {
return !/node_modules/.test(name);
}
}, console.log);
Known issues
Windows, node < v4.2.5
- Failed to detect
remove
event - Failed to get deleted filename or directory name
Misc
1. Watch multiple files or directories in one place
watch(['file1', 'file2'], console.log);
2. Other ways to filter
a) filtering directly inside the callback function:
watch('./', { recursive: true }, function(evt, name) {
if (!/node_modules/.test(name)) {
}
});
b) filtering with higher order function:
function filter(pattern, fn) {
return function(evt, name) {
if (pattern.test(name)) {
fn.apply(null, arguments);
}
}
}
watch('./', filter(/\.js$/, console.log));
3. customize watch command line tool
#!/usr/bin/env node
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 chrome
License
MIT
Copyright (c) 2012-2017 yuanchuan