Skip to main content
watch - fs - Node documentation
function watch

Usage in Deno

import { watch } from "node:fs";
watch(
filename: PathLike,
options: (WatchOptions & { encoding: "buffer"; }) | "buffer",
listener?: WatchListener<Buffer>,
): 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). eventTypeis 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.

If a signal is passed, aborting the corresponding AbortController will close the returned fs.FSWatcher.

Parameters

filename: PathLike
options: (WatchOptions & { encoding: "buffer"; }) | "buffer"
optional
listener: WatchListener<Buffer>

Return Type

watch(
filename: PathLike,
options?:
WatchOptions
| BufferEncoding
| null
,
listener?: WatchListener<string>,
): FSWatcher

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
| null

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.

optional
listener: WatchListener<string>

Return Type

watch(
filename: PathLike,
options: WatchOptions | string,
listener?: WatchListener<string | Buffer>,
): FSWatcher

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.

optional
listener: WatchListener<string | Buffer>

Return Type

watch(
filename: PathLike,
listener?: WatchListener<string>,
): FSWatcher

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
listener: WatchListener<string>

Return Type