Skip to main content
Bun includes a built-in file watcher that automatically restarts your application when files change, with support for hot module reloading for supported frameworks.

Quick Start

Run with watch mode:
Watch mode
Any changes to imported files trigger an automatic restart.

Watch Mode Features

Auto Restart

Automatically restarts on file changes

Fast Reloads

Optimized for sub-100ms restart times

Smart Detection

Only watches imported files, not entire project

Cross-Platform

Works on macOS, Linux, and Windows

Basic Usage

CLI Flag

Watch flag

Watch Patterns

Watch specific patterns

Hot Module Reloading

For React and other frameworks, use --hot for hot module reloading:
Hot reload
Hot reload features:
  • Preserves application state
  • Updates modules without full restart
  • React Fast Refresh support
  • Framework-agnostic HMR API
Implementation: src/runtime.zig:149 - Hot module reloading flag

Watch Mode Implementation

Bun’s watcher uses platform-specific APIs:

macOS (kqueue)

Watcher platform (src/Watcher.zig:143-148)
kqueue features:
  • File descriptor-based watching
  • Low latency notifications
  • Directory monitoring
Implementation: src/watcher/KEventWatcher.zig

Linux (inotify)

inotify features:
  • Kernel-level file monitoring
  • Efficient event batching
  • Recursive directory watching
Implementation: src/watcher/INotifyWatcher.zig

Windows

ReadDirectoryChangesW features:
  • Native Windows file watching
  • Buffer-based event delivery
  • Directory tree monitoring
Implementation: src/watcher/WindowsWatcher.zig

Watch Events

Watch events are tracked and delivered:
Watch events (src/Watcher.zig:1-49)
Event types:
  • File modified
  • File created
  • File deleted
  • File renamed

Configuration

Watch Ignore Patterns

Ignore specific files/directories:
Ignore patterns

Watch Include Patterns

Only watch specific files:
Include patterns

Programmatic Watcher

Create a watcher programmatically:
Programmatic watcher

Custom Watcher

Integrate Bun’s watcher:
Custom watcher integration

React Fast Refresh

Enable React Fast Refresh for hot reloading:
React Fast Refresh
Features:
  • Preserves component state
  • Updates on save
  • Error overlay
  • Automatic boundary detection
React Fast Refresh example
Implementation: src/runtime.zig:145-147 - React Fast Refresh feature

Performance

Debouncing

Bun debounces file changes to avoid excessive reloads:
  • Default debounce: 50ms
  • Batches multiple changes: Single reload for rapid file saves
  • Smart filtering: Ignores non-imported files

Optimization Tips

  1. Ignore large directories
  2. Watch specific patterns
  3. Minimize file I/O in watch callbacks

Development Workflows

Backend Server

Backend watch
Express server
Edit routes → automatic server restart.

Frontend Development

Frontend watch
React app
Edit components → hot module reload.

Tests

Test watch
Runs tests on file changes.

Watcher Trace

Debug watcher events:
Watcher trace
Outputs:
  • Files being watched
  • Change events
  • Reload triggers
Implementation: src/Watcher.zig:98-108 - Trace initialization

Error Handling

Watcher Errors

Handle watcher initialization errors:
Error handling

Linux: Increase Watch Limit

Increase inotify limit

Advanced Features

Custom Watch Callback

Watch callback

Selective Watching

Only watch specific modules:
Selective watching

Integration Examples

Express + Watch

Express watch

Next.js-like Dev Server

Dev server

Troubleshooting

Watch Not Detecting Changes

  1. Check file is imported: Watcher only tracks imported files
  2. Check ignore patterns: File might be ignored
  3. Check watch limit (Linux): Increase fs.inotify.max_user_watches

High CPU Usage

  1. Ignore large directories: --ignore "node_modules/**"
  2. Reduce watch scope: --watch "src/**/*.ts"
  3. Increase debounce: Custom watcher with longer debounce

Slow Reloads

  1. Reduce imports: Minimize dependency graph
  2. Use hot reload: --hot instead of --watch
  3. Profile startup: Measure import time