aboutsummaryrefslogtreecommitdiff
path: root/docs/guides/read-file/watch.md
blob: 0c38a0ecef09dbe0d11b5909fc3744eb9e07b4f6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
---
name: Watch a directory for changes
---

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.

```ts
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`.

```ts
import { watch } from "fs";

const watcher = watch(
  import.meta.dir,
  { recursive: true },
  (event, filename) => {
    console.log(`Detected ${event} in ${filename}`);
  },
);
```

---

Using the `node:fs/promises` module, you can listen for changes using `for await...of` instead of a callback.

```ts
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.

```ts
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](/docs/api/binary-data#typedarray) for more information on working with `Uint8Array` and other binary data formats in Bun.
2023-07-07Update installation guide in `development.md` (#3532)Gravatar Artur Androsovych 1-6/+16 2023-07-07fix decoding invalid UTF-8 input (#3563)Gravatar Ai Hoshino 2-2/+11 2023-07-07Update types, partially fix `typecheck` (#3551)Gravatar Colin McDonnell 25-199/+270 2023-07-07Add obscure HTTP methods (#3553)Gravatar Jarred Sumner 2-17/+85 2023-07-07[node:fs] `read`, `write` - support large numbers and BigInt (#3556)Gravatar Jarred Sumner 3-27/+80 2023-07-06Remove unnecessary `@setRuntimeSafety(false)`Gravatar Jarred Sumner 1-8/+8 2023-07-06add `[dir]` to defaultGravatar Dylan Conway 2-3/+3 2023-07-06Update listGravatar Jarred Sumner 1-5/+5 2023-07-06Add util.toUSVStringGravatar Jarred Sumner 3-1/+27 2023-07-06Minify zlib because its hugeGravatar Jarred Sumner 2-2552/+2 2023-07-06Add missing export in `url`Gravatar Jarred Sumner 3-366/+3 2023-07-06Update trustedDeps docGravatar Colin McDonnell 2-2/+12 2023-07-06Update trustedDeps docGravatar Colin McDonnell 1-2/+12 2023-07-06refactor (#3543)Gravatar Ciro Spaciari 9-131/+53 2023-07-06fixes #3544 (#3549)Gravatar Dylan Conway 1-0/+1 2023-07-06Various docs updates (#3437)Gravatar Colin McDonnell 13-53/+355 2023-07-06fix query without slash (#3547)Gravatar Ciro Spaciari 2-10/+14 2023-07-06Fixes #3537 (#3539)Gravatar Jarred Sumner 1-10/+41 2023-07-05don't unwrap react below version `18.0.0` (#3538)Gravatar Dylan Conway 2-5/+24 2023-07-05fix callbacks on release version (#3531)Gravatar Ciro Spaciari 2-31/+27 2023-07-05Update websocket_http_client.zigGravatar Dylan Conway 1-0/+2 2023-07-05Fixes #3512 (#3526)Gravatar Jarred Sumner 9-38/+168 2023-07-05Fixes #3515 (#3523)Gravatar Jarred Sumner 4-190/+182 2023-07-05Fixes #3520 (#3522)Gravatar Jarred Sumner 3-23/+37 2023-07-05add envs on tests (#3518)Gravatar Ciro Spaciari 4-0/+20 2023-07-04Update build-idGravatar Jarred Sumner 1-1/+1 2023-07-04Fix build determinism issue (thanks to @alexlamsl)Gravatar Jarred Sumner 1-2/+2 2023-07-04boopGravatar Jarred Sumner 10-55/+55 2023-07-04use sengrid account on nodemailer test (#3517)bun-v0.6.13Gravatar Ciro Spaciari 2-14/+15 2023-07-04[tls] fix servername (#3513)Gravatar Ciro Spaciari 4-8/+109 2023-07-04Add alias for readBigUInt64BE ... (#3514)Gravatar Ai Hoshino 2-4/+58 2023-07-04reduce countGravatar Jarred Sumner 1-2/+2 2023-07-04bumpGravatar Jarred Sumner 3-2/+2 2023-07-04Fix crashGravatar Jarred Sumner 1-5/+8