promises.watch - Node documentation
function promises.watch

Usage in Deno

import { promises } from "node:fs";
const { watch } = promises;
watch(
filename: PathLike,
options: (WatchOptions & { encoding: "buffer"; }) | "buffer",
): AsyncIterable<FileChangeInfo<Buffer>>

Returns an async iterator that watches for changes on filename, where filenameis either a file or a directory.

const { watch } = require('node:fs/promises');

const ac = new AbortController();
const { signal } = ac;
setTimeout(() => ac.abort(), 10000);

(async () => {
  try {
    const watcher = watch(__filename, { signal });
    for await (const event of watcher)
      console.log(event);
  } catch (err) {
    if (err.name === 'AbortError')
      return;
    throw err;
  }
})();

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

All the caveats for fs.watch() also apply to fsPromises.watch().

Parameters

filename: PathLike
options: (WatchOptions & { encoding: "buffer"; }) | "buffer"

Return Type

AsyncIterable<FileChangeInfo<Buffer>>

of objects with the properties:

watch(
filename: PathLike,
options?: WatchOptions | BufferEncoding,
): AsyncIterable<FileChangeInfo<string>>

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

Parameters

filename: PathLike

A path to a file or directory. If a URL is provided, it must use the file: protocol.

optional
options: WatchOptions | BufferEncoding

Either the encoding for the filename provided to the listener, or an object optionally specifying encoding, persistent, and recursive options. If encoding is not supplied, the default of 'utf8' is used. If persistent is not supplied, the default of true is used. If recursive is not supplied, the default of false is used.

Return Type

AsyncIterable<FileChangeInfo<string>>
watch(
filename: PathLike,
options: WatchOptions | string,
): AsyncIterable<FileChangeInfo<string>> | AsyncIterable<FileChangeInfo<Buffer>>

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

Parameters

filename: PathLike

A path to a file or directory. If a URL is provided, it must use the file: protocol.

options: WatchOptions | string

Either the encoding for the filename provided to the listener, or an object optionally specifying encoding, persistent, and recursive options. If encoding is not supplied, the default of 'utf8' is used. If persistent is not supplied, the default of true is used. If recursive is not supplied, the default of false is used.

Return Type

AsyncIterable<FileChangeInfo<string>> | AsyncIterable<FileChangeInfo<Buffer>>