# @eggjs/watcher 中文文档

规范地址：https://npmcn.com/package/%40eggjs/watcher/

版本：5\.1\.0
npm 元数据更新时间：2026-10-07T08:21:08.300Z
中文译文更新时间：2026-11-13T09:45:42.805Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40eggjs/watcher
原文对照：https://npmcn.com/package/%40eggjs/watcher/?lang=original
项目主页：https://github.com/eggjs/egg/tree/next/plugins/watcher
源码仓库：https://github.com/eggjs/egg

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：19.1 kB，31 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- Node.js 要求：\>=22\.18\.0
- 运行依赖：3 个
- peer 依赖：egg 4\.2\.0
- 首次发布：2024-12-18T12:54:40.600Z
- 最新版本发布：2026-10-07T08:21:07.811Z
- 近 30 天下载量：5085（2026-10-13 至 2026-11-11，npm 官方下载统计）
- 已发布版本数：64（近 12 个月 33 个）

## 中文简介

鸡蛋文件监视器插件

## 安装

```sh
npm install @eggjs/watcher
```

也可以使用：`pnpm add @eggjs/watcher`、`yarn add @eggjs/watcher`、`bun add @eggjs/watcher`

## README

# @eggjs/观察者

[![NPM version](https://img.shields.io/npm/v/@eggjs/watcher.svg?style=flat-square)](https://npmjs.org/package/@eggjs/watcher) [![Known Vulnerabilities](https://snyk.io/test/npm/@eggjs/watcher/badge.svg?style=flat-square)](https://snyk.io/test/npm/@eggjs/watcher) [![npm download](https://img.shields.io/npm/dm/@eggjs/watcher.svg?style=flat-square)](https://npmjs.org/package/@eggjs/watcher) [![Node.js Version](https://img.shields.io/node/v/@eggjs/watcher.svg?style=flat)](https://nodejs.org/en/download/)

鸡蛋的文件监视器插件

## 使用量

在工人过程中:

### app.watcher.watch(路径,听众)

开始监视文件 .

-   路径( String- QArray): 文件路径
-   收听器( 功能): 文件更改调用

### app.watcher.unwatch(路径\[,收听者\])

停止观看文件 .

-   路径( String- QArray): 文件路径
-   收听器( 功能): 文件更改调用

在代理过程中:

### 代理. watch.watch(路径,收听器)

开始监视文件 .

-   路径( String- QArray): 文件路径
-   收听器( 功能): 文件更改调用

### 代理. watcher.unwatch (路径 \[,收听者\])

停止观看文件 .

-   路径( String- QArray): 文件路径
-   收听器( 功能): 文件更改调用

## 监视模式

### `development` 模式

有一个内在的 [开发模式](https://github.com/eggjs/watcher/blob/master/src/lib/event-sources/development.ts) 在本地工作( env 是) `local`) (中文(简体) ). 一旦磁盘上的文件被修改,它就会发出一个 `change` 立即行动.

### 自定义监视模式

说我们希望构建自定义事件源插件( package name : `egg-watcher-custom`,蛋浆。名称: `watcherCustom`) (中文(简体) ).

首先定义我们的自定义事件源 :

```ts
// {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);
    }
  }
}
```

事件源执行因运行环境而异. 在与流浪者,道克人,桑巴人或其他这种非标准的发展方式合作时,您应该使用不同的表 API 来指定您的工作对象.

然后将您的自定义事件源添加到配置 :

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

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

选择在您想要的内嵌中使用您的自定义监视模式 .

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

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

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

如果可能, 命名为 `egg-watcher-${customName}`(单位:千美元)`egg-watcher-vagrant` 建议(例如).

## 问题和建议

请打开一个问题 [这里](https://github.com/eggjs/egg/issues)。 。 。 .

## 许可证

[麻省理工学院](https://npmcn.com/package/%40eggjs/watcher/LICENSE)

## 贡献者

[![Contributors](https://contrib.rocks/image?repo=eggjs/egg)](https://github.com/eggjs/egg/graphs/contributors)

制造的 [贡献者-img](https://contrib.rocks/)。 。 。 .

## 常见问题

### @eggjs/watcher 是什么？

鸡蛋文件监视器插件

### 如何安装 @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 和提交记录判断。

