文件名参数


【Filename argument】

在回调中提供 filename 参数仅在 Linux、macOS、Windows 和 AIX 上受支持。即使在受支持的平台上,也不能保证始终提供 filename。因此,不要假设回调中始终会提供 filename 参数,如果它为 null,需要有一些备用逻辑。

【Providing filename argument in the callback is only supported on Linux, macOS, Windows, and AIX. Even on supported platforms, filename is not always guaranteed to be provided. Therefore, don't assume that filename argument is always provided in the callback, and have some fallback logic if it is null.】

import { watch } from 'node:fs';
watch('somedir', (eventType, filename) => {
  console.log(`event type is: ${eventType}`);
  if (filename) {
    console.log(`filename provided: ${filename}`);
  } else {
    console.log('filename not provided');
  }
});