npm 中文文档 npm 中文文档
指南
npmjs.com (opens new window)
指南
npmjs.com (opens new window)
  • 快速入门

    • npm 是什么?
    • npm 安装和更新
    • npm 防止权限错误
    • npm package.json 文件
    • npm 安装包
    • npm 更新包
    • npm 卸载包
    • npm 创建 Node.js 模块
    • npm 发布和更新包
    • npm 使用语义化版本
    • npm 使用 Dist-tags 标记包
    • npm 包和模块的了解
  • 命令行
  • 配置 npm

Chokidar CLI


Build Status

Fast cross-platform command line utility to watch file system changes.

The underlying watch library is Chokidar, which is one of the best watch utilities for Node. Chokidar is battle-tested:

It is used in brunch, gulp, karma, PM2, browserify, webpack, BrowserSync, socketstream, derby, and many others. It has proven itself in production environments.


Prerequisites


Node.js v8.10.0 or newer

Install


If you need it only with npm scripts:

  1. ``` shell
  2. npm install chokidar-cli
  3. ```

Or globally

  1. ``` shell
  2. npm install -g chokidar-cli
  3. ```

Usage


Chokidar can be invoked using the chokidar command, without the -cli suffix.

Arguments use the form of runtime flags with string parameters, delimited by quotes. While in principal both single and double quotes are supported by chokidar-cli, the actual command line argument parsing is dependent on the operating system and shell used; for cross-platform compatibility, use double quotes (with escaping, if necessary), as single quotes are not universally supported by all operating systems.

This is particularly important when using chokidar-cli for run scripts specified in package.json. For maximum platform compatibility, make sure to use escaped double quotes around chokidar's parameters:

  1. ``` json
  2. "run": {
  3.   "chokidar": "chokidar \"**/*.js\" -c \"...\"
  4. },
  5. ```

Default behavior


By default chokidar streams changes for all patterns to stdout:

  1. ``` shell
  2. $ chokidar "**/*.js" "**/*.less"
  3. change:test/dir/a.js
  4. change:test/dir/a.less
  5. add:test/b.js
  6. unlink:test/b.js
  7. ```

Each change is represented with format event:relativepath. Possible events: add, unlink, addDir, unlinkDir, change.

Output only relative paths on each change

  1. ``` shell
  2. $ chokidar "**/*.js" "**/*.less" | cut -d ":" -f 2-
  3. test/dir/a.js
  4. test/dir/a.less
  5. test/b.js
  6. test/b.js
  7. ```

Run npm run build-jswhenever any .js file changes in the current work directory tree

`chokidar "*/.js" -c "npm run build-js"`

Watching in network directories must use polling

`chokidar "*/.less" -c "npm run build-less" --polling`

Pass the path and event details in to your custom command

`chokidar "*/.less" -c "if [ '{event}' = 'change' ]; then npm run build-less -- {path}; fi;"`

Detailed help

  1. ``` sh
  2. Usage: chokidar <pattern> [<pattern>...] [options]

  3. <pattern>:
  4. Glob pattern to specify files to be watched.
  5. Multiple patterns can be watched by separating patterns with spaces.
  6. To prevent shell globbing, write pattern inside quotes.
  7. Guide to globs: https://github.com/isaacs/node-glob#glob-primer

  8. Options:
  9.   -c, --command           Command to run after each change. Needs to be
  10.                           surrounded with quotes when command contains spaces.
  11.                           Instances of `{path}` or `{event}` within the command
  12.                           will be replaced by the corresponding values from the
  13.                           chokidar event.
  14.   -d, --debounce          Debounce timeout in ms for executing command
  15.                                                                   [default: 400]
  16.   -t, --throttle          Throttle timeout in ms for executing command
  17.                                                                   [default: 0]
  18.   -s, --follow-symlinks   When not set, only the symlinks themselves will be
  19.                           watched for changes instead of following the link
  20.                           references and bubbling events through the links path
  21.                                                       [boolean] [default: false]
  22.   -i, --ignore            Pattern for files which should be ignored. Needs to be
  23.                           surrounded with quotes to prevent shell globbing. The
  24.                           whole relative or absolute path is tested, not just
  25.                           filename. Supports glob patterns or regexes using
  26.                           format: /yourmatch/i
  27.   --initial               When set, command is initially run once
  28.                                                       [boolean] [default: false]
  29.   -p, --polling           Whether to use fs.watchFile(backed by polling) instead
  30.                           of fs.watch. This might lead to high CPU utilization.
  31.                           It is typically necessary to set this to true to
  32.                           successfully watch files over a network, and it may be
  33.                           necessary to successfully watch files in other non-
  34.                           standard situations         [boolean] [default: false]
  35.   --poll-interval         Interval of file system polling. Effective when --
  36.                           polling is set                          [default: 100]
  37.   --poll-interval-binary  Interval of file system polling for binary files.
  38.                           Effective when --polling is set         [default: 300]
  39.   --verbose               When set, output is more verbose and human readable.
  40.                                                       [boolean] [default: false]
  41.   --silent                When set, internal messages of chokidar-cli won't be
  42.                           written.                    [boolean] [default: false]
  43.   -h, --help              Show help                                    [boolean]
  44.   -v, --version           Show version number                          [boolean]

  45. Examples:
  46.   chokidar "**/*.js" -c "npm run build-js"  build when any .js file changes
  47.   chokidar "**/*.js" "**/*.less"            output changes of .js and .less
  48.                                             files

  49. ```

License


MIT
Last Updated: 2023-07-06 08:12:41