Chokidar CLI
Chokidar CLI is an ultra-fast, cross-platform command-line utility designed to watch file system changes and execute arbitrary commands or stream event output. It is built upon the robust and widely adopted `chokidar` library, which is known for its battle-tested stability and efficient file watching across various operating systems, handling quirks of `fs.watch` and `fs.watchFile` by normalizing events and supporting atomic/chunked writes. Currently at version 3.0.0, this tool provides a simple interface for developers to integrate automated tasks, such as compilation or testing, directly into their workflow upon file modifications. While the underlying `chokidar` library has seen recent updates (v4 and v5 as of late 2024/2025), `chokidar-cli` itself has not had a major release since v3.0.0 in July 2021, suggesting a slower release cadence for the CLI wrapper. Its key differentiators lie in abstracting the complexities of file system event handling and providing a straightforward command-line interface for common automation needs without requiring programmatic setup.
Common errors
-
command not found: chokidar-cli
cause The command line utility is named `chokidar`, not `chokidar-cli`, even though the npm package is `chokidar-cli`.fixUse `chokidar` instead of `chokidar-cli` in your terminal or scripts. Example: `chokidar "./**/*.js" -c "echo File changed"`. -
Error: EMFILE: too many open files, watch
cause The operating system's limit for open file descriptors has been reached. This often happens when watching a very large number of files/directories, or when a tool that uses `chokidar` (like `chokidar-cli`) is misconfigured to watch too broadly.fixIncrease your system's file descriptor limit (e.g., `ulimit -n 2048` on Linux/macOS for the current session, or modify system configuration for permanent changes). Alternatively, refine your glob patterns to watch fewer files and directories, or use `chokidar`'s `ignored` option to exclude unnecessary paths. -
Command not running or syntax error in command specified with -c
cause Incorrect quoting or shell interpretation of the command string, especially when it contains spaces, variables, or other special characters.fixEnsure the command passed to `-c` is enclosed in double quotes. If the command itself contains double quotes (e.g., for string literals), escape them properly. Example for `package.json`: `"watch:build": "chokidar \"src/**/*.ts\" -c \"npm run compile -- --path={path}\""`.
Warnings
- breaking When specifying patterns and commands, especially within `package.json` scripts, cross-platform compatibility requires the use of double quotes (escaped if necessary) around arguments. Single quotes may not be universally supported across all operating systems and shells, leading to parsing issues.
- gotcha The package name is `chokidar-cli`, but the command line utility itself is invoked as `chokidar` (without the `-cli` suffix). Using `chokidar-cli` on the command line will result in a 'command not found' error.
- gotcha Watching files on network directories (e.g., NFS, SMB shares) or within certain Docker environments often requires using the `--polling` option for reliable change detection, as native file system events may not propagate correctly.
- deprecated The underlying `chokidar` library has moved to ESM-only and increased its minimum Node.js requirement to v20+ in its v5 release (Nov 2025). While `chokidar-cli` v3.0.0 only requires Node.js >=8.10.0, it uses an older version of `chokidar`. Compatibility with future `chokidar-cli` versions may require updated Node.js environments and changes if `chokidar-cli` itself updates to `chokidar` v4/v5.
Install
-
npm install chokidar-cli -
yarn add chokidar-cli -
pnpm add chokidar-cli
Imports
- chokidar
import { chokidar } from 'chokidar-cli'npm install -g chokidar-cli # Then use the 'chokidar' command in your terminal
- chokidar
const chokidar = require('chokidar-cli')npm install chokidar-cli # Then use in package.json scripts: # {"scripts": {"watch:js": "chokidar \"**/*.js\" -c \"npm run build-js\""}} - chokidar
chokidar-cli "src/**/*.less" -c "npm run build-less -- {path}"chokidar "src/**/*.less" -c "npm run build-less -- {path}"
Quickstart
#!/bin/bash
# Install chokidar-cli globally for easy command access
npm install -g chokidar-cli
# Create a dummy project directory and a file
mkdir -p my-watched-project/src
echo "// Initial content" > my-watched-project/src/app.js
# Navigate into the project directory
cd my-watched-project
echo "Watching for changes in src/**/*.js. Try modifying src/app.js..."
# Run chokidar to watch JavaScript files and execute a simple echo command
# The -c flag runs a command, and {event} and {path} are placeholders
chokidar "src/**/*.js" -c "echo 'File {path} was {event}ed!' && ls -la src"
# To stop watching, press Ctrl+C in the terminal where chokidar is running.
# Example of how to trigger an event (run this in a separate terminal after starting chokidar):
# echo "console.log('updated content');" >> src/app.js
# rm src/app.js
# echo "// New file" > src/another.js