'change' 事件


  • eventType <string> 发生的变更事件的类型
  • filename <string> | <Buffer> 更改的文件名(如果相关/可用)

当监视的目录或文件中的某些内容发生更改时触发。 在 fs.watch() 中查看更多详细信息。

根据操作系统支持,可能不提供 filename 参数。 如果提供了 filename,如果 fs.watch() 在其 encoding 选项设置为 'buffer' 的情况下被调用,它将作为 <Buffer> 提供,否则 filename 将是 UTF-8 字符串。

import { watch } from 'node:fs';
// 通过 fs.watch() 监听器处理的示例
watch('./tmp', { encoding: 'buffer' }, (eventType, filename) => {
  if (filename) {
    console.log(filename);
    // 打印: <Buffer ...>
  }
});
  • eventType <string> The type of change event that has occurred
  • filename <string> | <Buffer> The filename that changed (if relevant/available)

Emitted when something changes in a watched directory or file. See more details in fs.watch().

The filename argument may not be provided depending on operating system support. If filename is provided, it will be provided as a <Buffer> if fs.watch() is called with its encoding option set to 'buffer', otherwise filename will be a UTF-8 string.

import { watch } from 'node:fs';
// Example when handled through fs.watch() listener
watch('./tmp', { encoding: 'buffer' }, (eventType, filename) => {
  if (filename) {
    console.log(filename);
    // Prints: <Buffer ...>
  }
});