watch

external fun watch(filename: PathLike, options: _WatchOptionsWithStringEncoding? = definedExternally, listener: WatchListener<String> = definedExternally): FSWatcher(source)
external fun watch(filename: PathLike, options: BufferEncoding? = definedExternally, listener: WatchListener<String> = definedExternally): FSWatcher(source)

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.

Since

v0.5.10

Parameters

listener

external fun watch(filename: PathLike, options: _WatchOptionsWithBufferEncoding, listener: WatchListener<Buffer<*>>): FSWatcher(source)
external fun watch(filename: PathLike, options: String, listener: WatchListener<Buffer<*>>): FSWatcher(source)
external fun watch(filename: PathLike, options: _WatchOptions?, listener: WatchListener<Any>): FSWatcher(source)
external fun watch(filename: PathLike, options: BufferEncoding?, listener: WatchListener<Any>): FSWatcher(source)
external fun watch(filename: PathLike, options: String?, listener: WatchListener<Any>): FSWatcher(source)
external fun watch(filename: PathLike, listener: WatchListener<String>): FSWatcher(source)
external fun watch(filename: PathLike, options: WatchOptionsWithBufferEncoding): ERROR CLASS: Symbol not found for js.iterable.AsyncIterator<node/fs/FileChangeInfo<node/buffer/Buffer<*>>>(source)
external fun watch(filename: PathLike, options: String): ERROR CLASS: Symbol not found for js.iterable.AsyncIterator<node/fs/FileChangeInfo<node/buffer/Buffer<*>>>(source)
external fun watch(filename: PathLike, options: WatchOptions): ERROR CLASS: Symbol not found for js.iterable.AsyncIterator<node/fs/FileChangeInfo<kotlin/Any>>(source)


external fun watch(filename: PathLike): ERROR CLASS: Symbol not found for js.iterable.AsyncIterator<node/fs/FileChangeInfo<kotlin/String>>(source)
external fun watch(filename: PathLike, options: WatchOptionsWithStringEncoding = definedExternally): ERROR CLASS: Symbol not found for js.iterable.AsyncIterator<node/fs/FileChangeInfo<kotlin/String>>(source)
external fun watch(filename: PathLike, options: BufferEncoding = definedExternally): ERROR CLASS: Symbol not found for js.iterable.AsyncIterator<node/fs/FileChangeInfo<kotlin/String>>(source)

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

import { watch } from '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().

Since

v15.9.0, v14.18.0

Return

of objects with the properties: