@eggjs/watcher

中文文档
5.1.0发布于 2026/10/07MITTypeScriptES Module构建来源已验证近 30 天下载 5085

file watcher plugin for egg

原始使用文档
文档目录11 节
  1. Usage
  2. app.watcher.watch(path, listener)
  3. app.watcher.unwatch(path[, listener])
  4. agent.watcher.watch(path, listener)
  5. agent.watcher.unwatch(path[, listener])
  6. Watching mode
  7. development Mode
  8. Customize Watching Mode
  9. Questions & Suggestions
  10. License
  11. Contributors

@eggjs/watcher

NPM version Known Vulnerabilities npm download Node.js Version

File watcher plugin for egg

Usage

In worker process:

app.watcher.watch(path, listener)

Start watching file(s).

  • path(String|Array): file path(s)
  • listener(Function): file change callback

app.watcher.unwatch(path[, listener])

Stop watching file(s).

  • path(String|Array): file path(s)
  • listener(Function): file change callback

In agent process:

agent.watcher.watch(path, listener)

Start watching file(s).

  • path(String|Array): file path(s)
  • listener(Function): file change callback

agent.watcher.unwatch(path[, listener])

Stop watching file(s).

  • path(String|Array): file path(s)
  • listener(Function): file change callback

Watching mode

development Mode

There's a built-in development mode which works in local(env is local). Once files on disk is modified it will emit a change event immediately.

Customize Watching Mode

Say we want to build a custom event source plugin (package name: egg-watcher-custom, eggPlugin.name: watcherCustom).

Firstly define our custom event source like this:

// {plugin_root}/lib/custom_event_source.js
import { Base } from 'sdk-base';

export default class CustomEventSource extends Base {
  // `opts` comes from app.config[${eventSourceName}]
  // `eventSourceName` will be registered later in
  // `config.watcher.eventSources` as the key shown below
  constructor(opts) {
    super(opts);
    this.ready(true);
  }

  watch(path) {
    // replace this with your desired way of watching,
    // when aware of any change, emit a `change` event
    // with an info object containing `path` property
    // specifying the changed directory or file.
    this._h = setInterval(() => {
      this.emit('change', { path });
    }, 1000);
  }

  unwatch() {
    // replace this with your implementation
    if (this._h) {
      clearInterval(this._h);
    }
  }
}

Event source implementations varies according to your running environment. When working with vagrant, docker, samba or such other non-standard way of development, you should use a different watch API specific to what you are working with.

Then add your custom event source to config:

// config/config.default.js
import CustomEventSource from '../lib/custom_event_source';

export default {
  watcher: {
    eventSources: {
      custom: CustomEventSource,
    },
  },
};

Choose to use your custom watching mode in your desired env.

// config/config.${env}.js

export default {
  watcher: {
    type: 'custom',
  },

  // this will pass to your CustomEventSource constructor as opts
  watcherCustom: {
    // foo: 'bar',
  },
};

If possible, plugins named like egg-watcher-${customName}(egg-watcher-vagrant eg.) are recommended.

Questions & Suggestions

Please open an issue here.

License

MIT

Contributors

Contributors

Made with contributors-img.

版本与发布记录

@eggjs/watcher 共发布 64 个版本,首次发布于 2024/12/18;过去 12 个月发布了 33 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
5.1.0latest2026/10/071 个月前
5.0.22026/10/061 个月前
5.0.2-rc.1rc2026/10/061 个月前
5.0.2-rc.02026/09/231 个月前
5.0.2-beta.27beta2026/09/201 个月前
5.0.2-beta.262026/08/073 个月前

关于 @eggjs/watcher 的常见问题

@eggjs/watcher 是什么?

file watcher plugin for egg

如何安装 @eggjs/watcher?

在项目目录运行 npm install @eggjs/watcher,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:egg(4.2.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@eggjs/watcher 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@eggjs/watcher 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@eggjs/watcher 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22.18.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@eggjs/watcher 可以商用吗?

@eggjs/watcher 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@eggjs/watcher 的下载量有多少?

根据 npm 官方下载统计,@eggjs/watcher 在 2026/10/13 – 2026/11/11 这 30 天内被下载 5,085 次,日均约 170 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@eggjs/watcher 还在维护吗?

最新版本 5.1.0 发布于 2026/10/07(1 个月前);至今共发布 64 个版本,其中过去 12 个月发布了 33 个;首次发布于 2024/12/18。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。