If it works, why use something else? https://conduitry.dev/cheap-watch
Go to file
2019-08-27 19:44:10 -04:00
src style 2019-06-07 12:46:56 -04:00
types add typescript declaration file 2018-11-13 07:16:46 -05:00
.gitignore update .gitignore for pnpm v3 2019-05-31 16:10:14 -04:00
.prettierrc.yaml add typescript compilation mechanism 2018-10-11 07:47:15 -04:00
.travis.yml travis adjustments 2018-10-24 04:18:44 -04:00
CHANGELOG.md v1.0.2 2018-11-13 07:18:37 -05:00
LICENSE docs 2018-04-02 17:17:52 -04:00
package.json pin @types/node to 8 2019-08-27 19:44:10 -04:00
README.md update readme 2018-12-10 15:58:25 -05:00
rollup.config.js style 2019-06-07 12:46:56 -04:00
test.js make tests stricter for file events 2018-10-24 06:54:46 -04:00
tsconfig.json add typescript compilation mechanism 2018-10-11 07:47:15 -04:00

Cheap Watch: If it works, why use something else?

npm version Build Status

Cheap Watch is a small, simple, dependency-free, cross-platform file system watcher for Node.js 8+.


new CheapWatch({ dir, filter, watch = true, debounce = 10 })

  • dir - The directory whose contents to watch. It's recommended, though not required, for this to be an absolute path, say one returned by path.resolve.
  • filter({ path, stats }) - (optional) A function to decide whether a given file or directory should be watched. It's passed an object containing the file or directory's relative path and its stats. It should return true or false (or a Promise resolving to one of those). Returning false for a directory means that none of its contents will be watched.
  • watch - (optional) Whether to actually watch the directory for changes. Defaults to true. If false, you can retrieve all of the files and directories within a given directory along with their initial Stats but changes will not be monitored.
  • debounce - (optional) Length of timeout in milliseconds to use to debounce incoming events from fs.watch. Defaults to 10. Multiple events are often emitted for a single change, and events can also be emitted before fs.stat reports the changes. So we will wait until debounce milliseconds have passed since the last fs.watch event for a file or directory before handling it. The default of 10ms Works On My Machine.



Initialize the watcher, traverse the directory to find the initial files and directories, and set up watchers to look for changes.

This returns a Promise that resolves once the initial contents of the directory have been traversed and all of the watchers have been set up.


Close all FSWatcher instances, and stop watching for file changes.



A Map of the watched files and directories. Each key is a relative path from the CheapWatch's dir, and each value is a Stats object for the file or directory. Paths are always separated by forward slashes, regardless of platform. This Map is kept up to date as files are changed on disk.

You can use stats.isFile() and stats.isDirectory() to determine whether something is a file or a directory.


A CheapWatch is an EventEmitter, and emits two events to report a new, updated, or deleted file or directory.

+ { path, stats, isNew }

A + event is emitted whenever a watched file or directory is created or updated. It's emitted with an object containing a path string, a stats object, and an isNew boolean which will be true for newly created files and directories and false for updated ones.

- { path, stats }

A - event is emitted whenever a watched file or directory is deleted. It's emitted with an object containing a path string and a stats object. stats will be the most recent Stats collected for the file or directory before it was deleted.


import CheapWatch from 'cheap-watch';

const watch = new CheapWatch({ dir, /* ... */ });

await watch.init();

for (const [path, stats] of watch.paths) {
	/* ... */

watch.on('+', ({ path, stats, isNew }) => { /* ... */ });
watch.on('-', ({ path, stats }) => { /* ... */ });