watch vs chokidar vs gaze vs node-watch vs nodemon
File Watching Libraries in Node.js
watchchokidargazenode-watchnodemonSimilar Packages:

File Watching Libraries in Node.js

File watching libraries are essential tools in Node.js development that allow developers to monitor changes in the filesystem. These libraries can detect modifications to files and directories, enabling automated tasks such as rebuilding assets, reloading servers, or triggering other actions based on file changes. They enhance the development workflow by providing real-time feedback and reducing the need for manual intervention when files are updated.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
watch884,7661,282-599 years agoApache-2.0
chokidar011,96382.1 kB354 months agoMIT
gaze01,155-688 years agoMIT
node-watch034126.1 kB83 years agoMIT
nodemon026,698219 kB9a month agoMIT

Feature Comparison: watch vs chokidar vs gaze vs node-watch vs nodemon

Performance

  • watch:

    Watch is a simple utility that performs well for basic tasks. However, it may not be suitable for high-performance needs or large-scale applications.

  • chokidar:

    Chokidar is designed for high performance, utilizing native file system events where available, which makes it faster and more efficient than many alternatives. It can handle thousands of files with minimal overhead, making it ideal for large projects.

  • gaze:

    Gaze offers decent performance for smaller projects, but it may not be as efficient as Chokidar when dealing with a large number of files. It uses polling as a fallback, which can lead to higher resource usage in larger applications.

  • node-watch:

    Node-watch is lightweight and performs well for basic use cases, but its performance may degrade with a significant number of files due to its reliance on polling in some environments.

  • nodemon:

    Nodemon is optimized for development environments, automatically restarting the server when files change. While it is not a file watcher per se, its performance is adequate for most development scenarios, focusing on server-side applications.

Ease of Use

  • watch:

    Watch is designed for simplicity and ease of integration into scripts. It has a basic API that is easy to understand and use for simple tasks.

  • chokidar:

    Chokidar provides a rich API that is easy to use, with clear documentation and examples. It supports advanced features like debouncing and throttling, making it user-friendly for complex scenarios.

  • gaze:

    Gaze is known for its simplicity and ease of setup. It has a straightforward API that is easy to understand, making it a great choice for beginners or quick scripts.

  • node-watch:

    Node-watch has a minimalistic API that is easy to use for simple file watching tasks. It is straightforward but may lack some advanced features found in other libraries.

  • nodemon:

    Nodemon is very easy to use, requiring minimal configuration. It automatically watches for changes in your application files and restarts the server, making it perfect for development.

Feature Set

  • watch:

    Watch is a basic utility that offers simple file watching capabilities without advanced features. It is suitable for straightforward tasks but may not meet the needs of more complex projects.

  • chokidar:

    Chokidar offers a comprehensive feature set, including support for file and directory watching, event debouncing, and the ability to ignore specific files or directories. It also supports both polling and native file system events, providing flexibility based on the environment.

  • gaze:

    Gaze provides basic file watching capabilities and supports glob patterns for file matching. However, it lacks some advanced features found in Chokidar, making it less versatile for complex scenarios.

  • node-watch:

    Node-watch is a minimalistic library that focuses on basic file watching functionality. It does not offer advanced features like event debouncing or file ignoring, making it suitable for simpler use cases.

  • nodemon:

    Nodemon primarily focuses on restarting Node.js applications on file changes. It does not provide extensive file watching features but is highly effective for development workflows.

Community and Support

  • watch:

    Watch has a smaller community, and while it is functional for basic tasks, it may not have as much support or as many resources available compared to more popular libraries.

  • chokidar:

    Chokidar has a strong community and is widely used in the Node.js ecosystem, ensuring good support and regular updates. Its popularity means that developers can find plenty of resources and examples online.

  • gaze:

    Gaze has a smaller community compared to Chokidar, which may result in fewer resources and examples. However, it is still maintained and has a dedicated user base.

  • node-watch:

    Node-watch is less popular than Chokidar, resulting in a smaller community and fewer resources. It is still functional for basic use cases but may lack extensive support.

  • nodemon:

    Nodemon has a large and active community, making it easy to find help and resources. Its popularity in the Node.js development community ensures that it is well-maintained and updated regularly.

Use Cases

  • watch:

    Watch is useful for simple scripts or build processes that require basic file watching capabilities without the need for advanced features.

  • chokidar:

    Chokidar is ideal for applications that require monitoring a large number of files or directories, such as build tools, asset pipelines, or any application needing real-time updates based on file changes.

  • gaze:

    Gaze is suitable for smaller projects or scripts that require simple file watching without the need for extensive configuration or features.

  • node-watch:

    Node-watch is best for lightweight applications or scripts that need basic file watching capabilities without additional complexity.

  • nodemon:

    Nodemon is perfect for Node.js application development, where immediate feedback is required upon file changes, allowing for a smooth development workflow.

How to Choose: watch vs chokidar vs gaze vs node-watch vs nodemon

  • watch:

    Choose Watch if you need a simple file watching utility that is easy to integrate into scripts or build processes. It is a straightforward option for basic file watching tasks without the need for extensive configuration.

  • chokidar:

    Choose Chokidar if you need a highly efficient and reliable file watcher that can handle large numbers of files with minimal resource usage. It is particularly suited for projects that require watching multiple files or directories and need to respond quickly to changes.

  • gaze:

    Select Gaze if you prefer a simple and straightforward API for watching files and directories, especially for smaller projects. It is easy to set up and use, making it a good choice for quick tasks or scripts that require file watching without the overhead of more complex libraries.

  • node-watch:

    Opt for Node-watch if you want a lightweight and minimalistic file watching solution. It is suitable for basic use cases where you need to monitor a few files or directories without additional features or complexity.

  • nodemon:

    Use Nodemon if you are developing Node.js applications and want an automatic server restart feature when files change. It is specifically designed for development environments and is ideal for projects where you need to see changes reflected immediately without manual restarts.

README for watch

watch -- Utilities for watching file trees in node.js

Install

  npm install watch

Purpose

The intention of this module is provide tools that make managing the watching of file & directory trees easier.

watch.watchTree(root, [options,] callback)

The first argument is the directory root you want to watch.

The options object is passed to fs.watchFile but can also be used to provide two additional watchTree specific options:

  • 'ignoreDotFiles' - When true this option means that when the file tree is walked it will ignore files that being with "."
  • 'filter' - You can use this option to provide a function that returns true or false for each file and directory to decide whether or not that file/directory is included in the watcher.
  • 'interval' - Specifies the interval duration in seconds, the time period between polling for file changes.
  • 'ignoreUnreadableDir' - When true, this options means that when a file can't be read, this file is silently skipped.
  • 'ignoreNotPermitted' - When true, this options means that when a file can't be read due to permission issues, this file is silently skipped.
  • 'ignoreDirectoryPattern' - When a regex pattern is set, e.g. /node_modules/, these directories are silently skipped.

The callback takes 3 arguments. The first is the file that was modified. The second is the current stat object for that file and the third is the previous stat object.

When a file is new the previous stat object is null.

When watchTree is finished walking the tree and adding all the listeners it passes the file hash (keys are the file/directory names and the values are the current stat objects) as the first argument and null as both the previous and current stat object arguments.

  watch.watchTree('/home/mikeal', function (f, curr, prev) {
    if (typeof f == "object" && prev === null && curr === null) {
      // Finished walking the tree
    } else if (prev === null) {
      // f is a new file
    } else if (curr.nlink === 0) {
      // f was removed
    } else {
      // f was changed
    }
  })

watch.unwatchTree(root)

Unwatch a previously watched directory root using watch.watchTree.

watch.createMonitor(root, [options,] callback)

This function creates an EventEmitter that gives notifications for different changes that happen to the file and directory tree under the given root argument.

The options object is passed to watch.watchTree.

The callback receives the monitor object.

The monitor object contains a property, files, which is a hash of files and directories as keys with the current stat object as the value.

The monitor has the following events.

  • 'created' - New file has been created. Two arguments, the filename and the stat object.
  • 'removed' - A file has been moved or deleted. Two arguments, the filename and the stat object for the fd.
  • 'changed' - A file has been changed. Three arguments, the filename, the current stat object, and the previous stat object.

The monitor can be stopped using .stop (calls unwatchTree).

  var watch = require('watch')
  watch.createMonitor('/home/mikeal', function (monitor) {
    monitor.files['/home/mikeal/.zshrc'] // Stat object for my zshrc.
    monitor.on("created", function (f, stat) {
      // Handle new files
    })
    monitor.on("changed", function (f, curr, prev) {
      // Handle file changes
    })
    monitor.on("removed", function (f, stat) {
      // Handle removed files
    })
    monitor.stop(); // Stop watching
  })

CLI

This module includes a simple command line interface, which you can install with npm install watch -g.

Usage: watch <command> [...directory] [OPTIONS]

OPTIONS:
    --wait=<seconds>
        Duration, in seconds, that watching will be disabled
        after running <command>. Setting this option will
        throttle calls to <command> for the specified duration.

    --filter=<file>
        Path to a require-able .js file that exports a filter
        function to be passed to watchTreeOptions.filter.
        Path is resolved relative to process.cwd().

    --interval=<seconds>
        Specifies the interval duration in seconds, the time period between polling for file changes.

    --ignoreDotFiles, -d
        Ignores dot or hidden files in the watch [directory].

     --ignoreUnreadable, -u
        Silently ignores files that cannot be read within the
        watch [directory].

     --ignoreDirectoryPattern=<regexp>, -p
        Silently skips directories that match the regular
        expression.

It will watch the given directories (defaults to the current working directory) with watchTree and run the given command every time a file changes.

Contributing

Releasing

On the latest clean master:

npm run release:major
npm run release:minor
npm run release:patch