事件:'change'


【Event: 'change'

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

当被监视的目录或文件发生变化时触发。详情请参见 fs.watch()

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

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

【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 ...>
  }
});