skills/bun-guides-read-file-watch/SKILL.md
Watch a directory for changes
npx skillsauth add jarle/bun-skills Bun Watch a directory for changesInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Bun implements the node:fs module, including the fs.watch function for listening for file system changes.
This code block listens for changes to files in the current directory. By default this operation is shallow, meaning that changes to files in subdirectories will not be detected.
import { watch } from "fs";
const watcher = watch(import.meta.dir, (event, filename) => {
console.log(`Detected ${event} in ${filename}`);
});
To listen to changes in subdirectories, pass the recursive: true option to fs.watch.
import { watch } from "fs";
const watcher = watch(import.meta.dir, { recursive: true }, (event, relativePath) => {
console.log(`Detected ${event} in ${relativePath}`);
});
Using the node:fs/promises module, you can listen for changes using for await...of instead of a callback.
import { watch } from "fs/promises";
const watcher = watch(import.meta.dir);
for await (const event of watcher) {
console.log(`Detected ${event.eventType} in ${event.filename}`);
}
To stop listening for changes, call watcher.close(). It's common to do this when the process receives a SIGINT signal, such as when the user presses Ctrl-C.
import { watch } from "fs";
const watcher = watch(import.meta.dir, (event, filename) => {
console.log(`Detected ${event} in ${filename}`);
});
process.on("SIGINT", () => {
// close watcher when Ctrl-C is pressed
console.log("Closing watcher...");
watcher.close();
process.exit(0);
});
Refer to API > Binary data > Typed arrays for more information on working with Uint8Array and other binary data formats in Bun.
development
Using TypeScript with Bun, including type definitions and compiler options
development
Learn how to write tests using Bun's Jest-compatible API with support for async tests, timeouts, and various test modifiers
testing
Learn how to use snapshot testing in Bun to save and compare output between test runs
testing
Learn about Bun test's runtime integration, environment variables, timeouts, and error handling