sane vs gaze vs node-watch vs filewatcher vs chokidar vs watch vs watchpack
File Watching Libraries for Node.js
sanegazenode-watchfilewatcherchokidarwatchwatchpackSimilar Packages:

File Watching Libraries for Node.js

File watching libraries are essential tools in Node.js development that allow applications to monitor changes in the filesystem. These libraries provide an interface to listen for events such as file creation, modification, and deletion, enabling developers to automate tasks like rebuilding assets, live reloading, or triggering other processes in response to file changes. By leveraging these libraries, developers can enhance their workflow efficiency and create more dynamic applications that respond to changes in real-time.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
sane4,496,214387-345 years agoMIT
gaze2,451,3471,155-688 years agoMIT
node-watch878,00434126.1 kB83 years agoMIT
filewatcher66,29854-510 years agoMIT
chokidar011,94182.1 kB343 months agoMIT
watch01,282-599 years agoApache-2.0
watchpack039595.7 kB13a month agoMIT

Feature Comparison: sane vs gaze vs node-watch vs filewatcher vs chokidar vs watch vs watchpack

Performance

  • sane:

    Sane is designed for performance and can efficiently watch large numbers of files. It combines native file system events with polling, ensuring responsiveness while maintaining low resource usage, making it suitable for medium to large projects.

  • gaze:

    Gaze offers decent performance for moderate file watching needs. It supports multiple file patterns and can handle changes effectively, but may not be the best choice for extremely large projects due to potential performance bottlenecks.

  • node-watch:

    Node-Watch is straightforward and performs well for basic file watching tasks. It is suitable for smaller applications but may struggle with performance when monitoring a large number of files due to its simpler design.

  • filewatcher:

    FileWatcher is lightweight and performs adequately for small to medium-sized projects. However, it may not be as efficient as other libraries when monitoring a large number of files or directories, as it relies on polling mechanisms.

  • chokidar:

    Chokidar is optimized for performance, utilizing native file system events to minimize CPU usage and memory overhead. It efficiently handles large directories and can watch thousands of files without significant slowdowns, making it ideal for production environments.

  • watch:

    Watch is simple and performs adequately for most use cases. However, it may not be optimized for handling extensive file systems, making it more suitable for smaller applications or scripts.

  • watchpack:

    Watchpack is built for high-performance file watching, especially in build systems like Webpack. It efficiently manages file changes and caching, making it ideal for large-scale applications that require fast rebuilds.

API Simplicity

  • sane:

    Sane provides a user-friendly API that balances simplicity and functionality. It is designed to be easy to use while still offering advanced features for more complex use cases.

  • gaze:

    Gaze offers a straightforward API that supports multiple file patterns and globbing. Its simplicity makes it easy to integrate into projects without a steep learning curve.

  • node-watch:

    Node-Watch features a minimalistic API that is easy to use for basic file watching tasks. It is ideal for developers who prefer simplicity over extensive configuration options.

  • filewatcher:

    FileWatcher has a very simple API that is easy to understand and use. It is suitable for quick setups and small projects, allowing developers to get started with minimal overhead.

  • chokidar:

    Chokidar provides a clean and intuitive API that makes it easy to set up file watching with minimal configuration. Its event-driven model allows developers to quickly respond to file changes, enhancing productivity.

  • watch:

    Watch has a very simple and direct API, making it easy to implement file watching in scripts or small applications. It is designed for developers who want a quick and straightforward solution.

  • watchpack:

    Watchpack has a more complex API tailored for advanced use cases, particularly in build systems. While it may require more initial setup, it offers powerful features for those needing fine-grained control over file watching.

Cross-Platform Support

  • sane:

    Sane is built to be cross-platform, ensuring that it can handle file watching tasks on Windows, macOS, and Linux without issues, making it a flexible option for various projects.

  • gaze:

    Gaze supports multiple platforms, ensuring that file watching works consistently regardless of the operating system. This is particularly useful for teams with diverse development setups.

  • node-watch:

    Node-Watch is cross-platform and functions well on major operating systems, making it suitable for developers who need a consistent experience across different environments.

  • filewatcher:

    FileWatcher is cross-platform and works well on different operating systems, making it a versatile choice for developers working in mixed environments.

  • chokidar:

    Chokidar is designed to work seamlessly across various operating systems, including Windows, macOS, and Linux. This makes it a reliable choice for projects that need to be developed in diverse environments.

  • watch:

    Watch is compatible with multiple platforms, providing a consistent file watching experience across different operating systems, which is beneficial for developers working in diverse environments.

  • watchpack:

    Watchpack is designed for high-performance file watching in build systems and is compatible with various platforms, ensuring that it can be integrated into different development environments.

Use Cases

  • sane:

    Sane is a good fit for medium to large projects that need efficient file watching with a balance of performance and ease of use, making it suitable for modern development workflows.

  • gaze:

    Gaze is perfect for projects that need to watch multiple files or directories with specific patterns, such as build tools or task runners that require monitoring of source files.

  • node-watch:

    Node-Watch is suitable for lightweight applications and scripts that require basic file watching capabilities without extensive configuration, making it ideal for quick setups.

  • filewatcher:

    FileWatcher is best suited for simple scripts and small projects where basic file watching is needed without the complexity of larger libraries, making it a good choice for quick tasks.

  • chokidar:

    Chokidar is ideal for applications that require real-time file watching, such as live reloading in development servers, build systems, and asset management tools, making it a versatile choice for many scenarios.

  • watch:

    Watch is best for small applications or scripts that require straightforward file watching without the need for advanced features, making it a practical choice for simple tasks.

  • watchpack:

    Watchpack is designed for build systems and complex applications that require high-performance file watching, particularly in environments like Webpack where fast rebuilds are essential.

How to Choose: sane vs gaze vs node-watch vs filewatcher vs chokidar vs watch vs watchpack

  • sane:

    Choose Sane if you need a library that balances performance and ease of use. It is designed to handle large numbers of files efficiently and provides a simple API for watching file changes, making it a good choice for medium-sized projects.

  • gaze:

    Opt for Gaze if you require a straightforward API with support for multiple file patterns and globbing. It is particularly useful for projects where you want to watch multiple files or directories with specific matching rules.

  • node-watch:

    Use Node-Watch for its simplicity and minimalistic approach. It is great for quick setups and provides basic file watching capabilities without the overhead of more complex libraries, making it suitable for small applications or scripts.

  • filewatcher:

    Select FileWatcher if you need a simple and lightweight solution for monitoring file changes without additional dependencies. It is easy to set up and works well for smaller projects or scripts that require basic file watching functionality.

  • chokidar:

    Choose Chokidar for its performance and efficiency, especially when dealing with large directories or numerous files. It uses native file system events where possible and falls back to polling when necessary, making it a robust choice for production environments.

  • watch:

    Select Watch for its straightforward implementation and compatibility with various platforms. It is ideal for developers looking for a no-frills solution to monitor file changes without extensive configuration.

  • watchpack:

    Use Watchpack if you are working with Webpack or require advanced features like caching and performance optimizations. It is designed for high-performance file watching in build systems and is particularly useful in larger applications.

README for sane

Try on repl.it CI

sane

I've been driven to insanity by node filesystem watcher wrappers. Sane aims to be fast, small, and reliable file system watcher. It does that by:

  • By default stays away from fs polling because it's very slow and cpu intensive
  • Uses fs.watch by default and sensibly works around the various issues
  • Maintains a consistent API across different platforms
  • Where fs.watch is not reliable you have the choice of using the following alternatives:

Install

$ npm install sane

How to choose a mode

Don't worry too much about choosing the correct mode upfront because sane maintains the same API across all modes and will be easy to switch.

  • If you're only supporting Linux and OS X, watchman would be the most reliable mode
  • If you're using node > v0.10.0 use the default mode
  • If you're running OS X and you're watching a lot of directories and you're running into https://github.com/joyent/node/issues/5463, use watchman
  • If you're in an environment where native file system events aren't available (like Vagrant), you should use polling
  • Otherwise, the default mode should work well for you

API

sane(dir, options)

Watches a directory and all its descendant directories for changes, deletions, and additions on files and directories.

var watcher = sane('path/to/dir', {glob: ['**/*.js', '**/*.css']});
watcher.on('ready', function () { console.log('ready') });
watcher.on('change', function (filepath, root, stat) { console.log('file changed', filepath); });
watcher.on('add', function (filepath, root, stat) { console.log('file added', filepath); });
watcher.on('delete', function (filepath, root) { console.log('file deleted', filepath); });
// close
watcher.close();

options:

  • glob: a single string glob pattern or an array of them.
  • poll: puts the watcher in polling mode. Under the hood that means fs.watchFile.
  • watchman: makes the watcher use watchman.
  • watchmanPath: sets a custom path for watchman binary.
  • watchexec: makes the watcher use watchexec.
  • dot: enables watching files/directories that start with a dot.
  • ignored: a glob, regex, function, or array of any combination.

For the glob pattern documentation, see micromatch. If you choose to use watchman you'll have to install watchman yourself). If you choose to use watchexec you'll have to install watchexec yourself). For the ignored options, see anymatch.

sane.NodeWatcher(dir, options)

The default watcher class. Uses fs.watch under the hood, and takes the same options as sane(dir, options).

sane.WatchmanWatcher(dir, options)

The watchman watcher class. Takes the same options as sane(dir, options).

sane.Watchexec(dir, options)

The watchexec watcher class. Takes the same options as sane(dir, options).

sane.PollWatcher(dir, options)

The polling watcher class. Takes the same options as sane(dir, options) with the addition of:

  • interval: indicates how often the files should be polled. (passed to fs.watchFile)

sane.{Node|Watchman|Watchexec|Poll}Watcher#close

Stops watching.

sane.{Node|Watchman|Watchexec|Poll}Watcher events

Emits the following events:

All events are passed the file/dir path relative to the root directory

  • ready when the program is ready to detect events in the directory
  • change when a file changes
  • add when a file or directory has been added
  • delete when a file or directory has been deleted

CLI

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

Usage: sane <command> [...directory] [--glob=<filePattern>] [--poll] [--watchman] [--watchman-path=<watchmanBinaryPath>] [--dot] [--wait=<seconds>]

OPTIONS:
    --glob=<filePattern>
      A single string glob pattern or an array of them.

    --ignored=<filePattern>
      A glob, regex, function, or array of any combination.

    --poll, -p
      Use polling mode.

    --watchman, -w
      Use watchman (if available).

    --watchman-path=<watchmanBinaryPath>
      Sets a custom path for watchman binary (if using this mode).

    --dot, -d
      Enables watching files/directories that start with a dot.

    --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.
    --quiet, -q
      Disables sane's console output

    --changes-only, -o
      Runs <command> only when a change occur. Skips running <command> at startup

It will watch the given directory and run the given every time a file changes.

CLI example usage

  • sane 'echo "A command ran"'
  • sane 'echo "A command ran"' --glob='**/*.css'
  • sane 'echo "A command ran"' site/assets/css --glob='**/*.css'
  • sane 'echo "A command ran"' --glob='**/*.css' --ignored='**/ignore.css'
  • sane 'echo "A command ran"' --wait=3
  • sane 'echo "A command ran"' -p

License

MIT

Credits

The CLI was originally based on the watch CLI. Watch is licensed under the Apache License Version 2.0.