Ultra-fast cross-platform command line utility to watch file system changes.
npx @tessl/cli install tessl/npm-chokidar-cli@3.0.0Chokidar CLI is an ultra-fast cross-platform command line utility for watching file system changes. It provides comprehensive file watching capabilities with configurable options for command execution, debouncing, throttling, and cross-platform compatibility. Built on top of the battle-tested chokidar library, it enables developers to monitor file modifications, additions, and deletions through glob patterns with support for executing custom commands when changes occur.
npm install chokidar-cli (locally) or npm install -g chokidar-cli (globally)For programmatic usage of the utils module:
const { run } = require("chokidar-cli/utils");# Watch JavaScript files and run build command
chokidar "**/*.js" -c "npm run build-js"
# Watch multiple file types
chokidar "**/*.js" "**/*.less"
# Watch with polling (for network directories)
chokidar "**/*.less" -c "npm run build-less" --polling
# Use path and event variables in commands
chokidar "**/*.less" -c "if [ '{event}' = 'change' ]; then npm run build-less -- {path}; fi;"const { run } = require("chokidar-cli/utils");
// Execute a cross-platform command
run("npm test")
.then(exitCode => {
console.log(`Command completed with exit code: ${exitCode}`);
})
.catch(error => {
console.error("Command failed:", error);
});
// Execute with options
run("ls -la", {
cwd: "/some/directory",
pipe: false,
callback: (child) => {
console.log(`Started process with PID: ${child.pid}`);
}
})
.then(exitCode => console.log("Done"))
.catch(console.error);The primary interface for chokidar-cli is its command line utility.
chokidar <pattern> [<pattern>...] [options]Parameters:
<pattern> - Glob pattern(s) to specify files to be watched. Multiple patterns can be separated by spaces.Options:
-c, --command <string> - Command to run after each change. Supports {path} and {event} placeholders-d, --debounce <number> - Debounce timeout in ms for executing command (default: 400)-t, --throttle <number> - Throttle timeout in ms for executing command (default: 0)-s, --follow-symlinks - Follow symbolic links instead of watching symlinks themselves (default: false)-i, --ignore <string> - Pattern for files to ignore. Supports glob patterns or regex format: /pattern/flags--initial - Run command initially once before watching (default: false)-p, --polling - Use fs.watchFile (polling) instead of fs.watch (default: false)--poll-interval <number> - Polling interval in ms when --polling is set (default: 100)--poll-interval-binary <number> - Binary file polling interval in ms when --polling is set (default: 300)--verbose - Verbose and human-readable output (default: false)--silent - Suppress internal chokidar-cli messages (default: false)-h, --help - Show help-v, --version - Show version informationUtility function for executing shell commands in a cross-platform manner.
/**
* Execute a shell command in a cross-platform manner
* @param {string} cmd - The command to execute
* @param {Object} [opts] - Configuration options
* @param {boolean} [opts.pipe=true] - Whether to pipe stdio to parent process
* @param {string} [opts.cwd] - Working directory for command execution
* @param {Function} [opts.callback] - Callback function called after successful spawn
* @returns {Promise<number>} Promise that resolves with exit code or rejects with error
*/
function run(cmd, opts);The run function automatically resolves the appropriate shell:
COMSPEC environment variable with /c flagSHELL environment variable with -c flagChokidar CLI monitors and reports the following file system events:
// Event types reported in output
type FileEvent = 'add' | 'addDir' | 'unlink' | 'unlinkDir' | 'change';Event Descriptions:
add - File addedaddDir - Directory addedunlink - File removedunlinkDir - Directory removedchange - File changedEvents are streamed to stdout in the format event:path:
change:src/app.js
add:src/components/Button.js
unlink:src/old-file.jsHuman-readable event descriptions with file paths:
File changed: src/app.js
File added: src/components/Button.js
File removed: src/old-file.jsNo output except for errors and the initial "Watching..." message.
interface DefaultOptions {
debounce: 400; // ms
throttle: 0; // ms
followSymlinks: false;
ignore: null;
polling: false;
pollInterval: 100; // ms
pollIntervalBinary: 300; // ms
verbose: false;
silent: false;
initial: false;
command: null;
}The --ignore option supports:
"**/node_modules/**""/\\.git/i" (case-insensitive regex matching .git)Multiple ignore patterns can be specified by using the option multiple times.
When using the --command option, the following placeholders are replaced:
{path} - Replaced with the file path that triggered the event{event} - Replaced with the event type (add, change, unlink, etc.)SHELL nor COMSPEC environment variables are set (error message: "$SHELL environment variable is not set.")All errors are written to stderr with descriptive messages and stack traces for debugging.
Chokidar CLI automatically handles cross-platform differences:
COMSPEC) vs Unix (SHELL) environments/c for Windows, -c for Unix)