'change' 事件


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

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

可能不提供 filename 参数,这取决于操作系统的支持。 如果提供了 filename,则当调用 fs.watch() 并将其 encoding 选项设置为 'buffer' 时, filename 将是一个 Buffer,否则 filename 将是 UTF-8 字符串。

// 使用 fs.watch()监听器的示例。
fs.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.

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