📜  chokidar npm - Shell-Bash (1)

📅  最后修改于: 2023-12-03 15:29:58.231000             🧑  作者: Mango

Chokidar npm - Shell/Bash

Chokidar is a highly efficient Node.js library that helps programmers to watch for changes in files and directories. It is a powerful tool for automating tasks, such as compiling and minifying code, running tests or deploying applications.

Installation

To install Chokidar, use the following command in your terminal:

npm install chokidar
Usage

To use Chokidar in your project, simply require it in your Node.js script:

const chokidar = require('chokidar');

You can then define a watcher that will execute the specified actions when certain files or directories change:

chokidar.watch('./src').on('all', (event, path) => {
  console.log(event, path);
});

The above code creates a watcher for the ./src directory that logs all events (e.g. 'add', 'change', 'unlink') and paths (e.g. './src/index.js') to the console.

Options

Chokidar provides a wide range of options that can be used to configure the watcher, such as:

  • ignoreInitial: Whether to ignore events that occur when the watcher is first set up (default false).
  • ignored: A regular expression, function or array that specifies which files or directories should be ignored by the watcher.
  • usePolling: Whether to use a polling mechanism instead of the native file watcher (default false on macOS and Linux, true on other platforms).
  • interval: The polling interval (in milliseconds) to use when usePolling is enabled (default 100).
  • binaryInterval: The polling interval (in milliseconds) to use when usePolling is enabled and watching binary files (default 300).
Conclusion

Chokidar is a powerful tool that can help you automate tasks and save time in your development workflow. With its advanced features and wide range of options, it is a valuable asset to any Node.js project. So go ahead and give it a try!