watch vs node-watch vs chokidar vs gaze vs nodemon
文件监视库
watchnode-watchchokidargazenodemon类似的npm包:

文件监视库

文件监视库用于监测文件系统中的变化,通常用于自动化任务,如重新编译代码、重新加载服务器等。它们通过监听文件的变化来触发相应的操作,极大提高了开发效率。这些库各有特点,适用于不同的使用场景和需求。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
watch884,7661,282-599 年前Apache-2.0
node-watch745,43734126.1 kB83 年前MIT
chokidar011,96382.1 kB354 个月前MIT
gaze01,155-688 年前MIT
nodemon026,698219 kB91 个月前MIT

功能对比: watch vs node-watch vs chokidar vs gaze vs nodemon

性能

  • watch:

    Watch是一个简单的工具,性能较低,适合快速的脚本和小型项目。

  • node-watch:

    Node-watch是一个轻量级的监视工具,性能良好,适合快速监视少量文件的变化。

  • chokidar:

    Chokidar是一个高性能的文件监视库,使用了操作系统的原生文件监视功能,能够高效地处理大量文件的变化,适合大型项目。

  • gaze:

    Gaze的性能相对较低,适合小型项目或少量文件的监视,使用简单,但在处理大量文件时可能会出现性能瓶颈。

  • nodemon:

    Nodemon专注于监视Node.js应用的文件变化,性能表现良好,能够快速重启服务器。

使用场景

  • watch:

    Watch适合快速的开发任务,适合小型项目的文件监视。

  • node-watch:

    Node-watch适合快速开发和简单的文件监视任务,如自动化脚本。

  • chokidar:

    Chokidar适用于需要高效监视大量文件变化的场景,如大型Web应用、构建工具等。

  • gaze:

    Gaze适合小型项目或简单的文件监视需求,如监视CSS或JS文件的变化。

  • nodemon:

    Nodemon专为Node.js开发设计,适合需要频繁修改代码并希望自动重启服务器的场景。

易用性

  • watch:

    Watch的使用非常直观,适合快速脚本和小型项目。

  • node-watch:

    Node-watch的使用非常简单,适合快速设置和使用。

  • chokidar:

    Chokidar提供了丰富的API和事件,易于使用,适合开发者进行复杂的文件监视。

  • gaze:

    Gaze的API简单明了,易于上手,适合初学者和小型项目。

  • nodemon:

    Nodemon的配置简单,开箱即用,适合快速开发。

扩展性

  • watch:

    Watch的扩展性较低,适合简单的监视需求。

  • node-watch:

    Node-watch的扩展性较低,适合简单的监视任务。

  • chokidar:

    Chokidar支持多种事件和选项,具有良好的扩展性,适合复杂的应用需求。

  • gaze:

    Gaze的扩展性有限,适合简单的监视需求,不适合复杂应用。

  • nodemon:

    Nodemon可以与其他工具结合使用,具有一定的扩展性,适合Node.js开发。

社区支持

  • watch:

    Watch的社区支持较少,适合简单的使用需求。

  • node-watch:

    Node-watch的社区支持有限,适合简单的使用场景。

  • chokidar:

    Chokidar拥有活跃的社区和丰富的文档支持,适合开发者使用。

  • gaze:

    Gaze的社区相对较小,文档支持一般,适合简单项目。

  • nodemon:

    Nodemon拥有广泛的社区支持和丰富的文档,适合Node.js开发者。

如何选择: watch vs node-watch vs chokidar vs gaze vs nodemon

  • watch:

    选择Watch如果你需要一个简单的文件监视工具,适合快速的脚本和小型项目,且不需要复杂的功能。

  • node-watch:

    选择Node-watch如果你需要一个轻量级的解决方案,能够快速设置并监视文件变化,适合简单的任务。

  • chokidar:

    选择Chokidar如果你需要一个高性能、功能丰富的文件监视库,支持多种平台和文件系统,且能够处理大量文件的变化。

  • gaze:

    选择Gaze如果你需要一个简单易用的库,适合小型项目,且支持多种文件模式的监视。

  • nodemon:

    选择Nodemon如果你需要在开发Node.js应用时自动重启服务器,特别是在代码文件发生变化时。

watch的README

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