chokidar
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
anymatch | 3.1.3 | 3.57 kB | ISC | prod | |
braces | 3.0.2 | 15.27 kB | MIT | prod | |
fsevents | 2.3.3 | 22.27 kB | MIT | prod optional | |
glob-parent | 5.1.2 | 4.74 kB | ISC | prod | |
is-binary-path | 2.1.0 | 1.58 kB | MIT | prod | |
is-glob | 4.0.3 | 4.16 kB | MIT | prod | |
normalize-path | 3.0.0 | 3.48 kB | MIT | prod | |
readdirp | 3.6.0 | 7.38 kB | MIT | prod |
Chokidar is a highly efficient and minimalistic cross-platform library in Node.js that is designed for file watching. It addresses the common issues associated with Node.js's fs.watch
and fs.watchFile
, which include not reporting filenames on MacOS, not reporting events when using certain editors on MacOS, often reporting events twice, and not supporting recursive watching among others. By using fs.watch
and fs.watchFile
for watching and normalizing the events it receives, Chokidar provides a robust solution to these problems. Chokidar is the library of choice for successful implementation in production environments for major software like Microsoft's Visual Studio Code, Gulp, and Webpack.
To use Chokidar, you need to install it via npm using the command npm install chokidar
. Once installed, you can import it into your project using require
and start watching files or directories as shown in the following example:
const chokidar = require('chokidar');
// One-liner for the current directory, logs all events
chokidar.watch('.').on('all', (event, path) => {
console.log(event, path);
});
// More complex example with event listeners
const watcher = chokidar.watch('file, dir, glob, or array', {
ignored: /(^|[\/\\])\../, // ignore dotfiles
persistent: true
});
const log = console.log.bind(console);
// Add event listeners
watcher
.on('add', path => log(`File ${path} has been added`))
.on('change', path => log(`File ${path} has been changed`))
.// ...
You can add new files to the watcher, get the list of actual paths being watched, stop watching certain files, and even stop watching completely using watcher.add()
, watcher.getWatched()
, watcher.unwatch()
, and watcher.close()
respectively.
The complete documentation and API reference for Chokidar can be found directly in its repository on GitHub at https://github.com/paulmillr/chokidar
. It provides a complete guide on how to use Chokidar's methods and options, handle events, use the CLI, as well as troubleshooting tips and more details about its inner workings.