fs.watch(filename[, options][, listener])

监视 filename 的更改,其中 filename 是文件或目录。

第二个参数是可选的。如果 options 是字符串,则它指定 encoding。 否则 options 应是对象。

监听器回调有两个参数 (eventType, filename)eventType'rename''change'filename 是触发事件的文件的名称。

在大多数平台上,只要文件名出现或在目录中消失,就会触发 'rename' 事件。

监听器回调绑定在由 fs.FSWatcher 触发的 'change' 事件上,但它与 eventType'change' 值不是一回事。

  • filename <string> | <Buffer> | <URL>
  • options <string> | <Object>

    • persistent <boolean> Indicates whether the process should continue to run as long as files are being watched. Default: true.
    • recursive <boolean> Indicates whether all subdirectories should be watched, or only the current directory. This applies when a directory is specified, and only on supported platforms (See Caveats). Default: false.
    • encoding <string> Specifies the character encoding to be used for the filename passed to the listener. Default: 'utf8'.
  • listener <Function> | <undefined> Default: undefined

  • Returns: <fs.FSWatcher>

Watch for changes on filename, where filename is either a file or a directory.

The second argument is optional. If options is provided as a string, it specifies the encoding. Otherwise options should be passed as an object.

The listener callback gets two arguments (eventType, filename). eventType is either 'rename' or 'change', and filename is the name of the file which triggered the event.

On most platforms, 'rename' is emitted whenever a filename appears or disappears in the directory.

The listener callback is attached to the 'change' event fired by fs.FSWatcher, but it is not the same thing as the 'change' value of eventType.