Quick Start
Run with watch mode:Watch mode
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
- Preserves application state
- Updates modules without full restart
- React Fast Refresh support
- Framework-agnostic HMR API
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)
- File descriptor-based watching
- Low latency notifications
- Directory monitoring
src/watcher/KEventWatcher.zig
Linux (inotify)
inotify features:- Kernel-level file monitoring
- Efficient event batching
- Recursive directory watching
src/watcher/INotifyWatcher.zig
Windows
ReadDirectoryChangesW features:- Native Windows file watching
- Buffer-based event delivery
- Directory tree monitoring
src/watcher/WindowsWatcher.zig
Watch Events
Watch events are tracked and delivered:Watch events (src/Watcher.zig:1-49)
- 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
- Preserves component state
- Updates on save
- Error overlay
- Automatic boundary detection
React Fast Refresh example
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
-
Ignore large directories
-
Watch specific patterns
- Minimize file I/O in watch callbacks
Development Workflows
Backend Server
Backend watch
Express server
Frontend Development
Frontend watch
React app
Tests
Test watch
Watcher Trace
Debug watcher events:Watcher trace
- Files being watched
- Change events
- Reload triggers
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
- Check file is imported: Watcher only tracks imported files
- Check ignore patterns: File might be ignored
- Check watch limit (Linux): Increase
fs.inotify.max_user_watches
High CPU Usage
- Ignore large directories:
--ignore "node_modules/**" - Reduce watch scope:
--watch "src/**/*.ts" - Increase debounce: Custom watcher with longer debounce
Slow Reloads
- Reduce imports: Minimize dependency graph
- Use hot reload:
--hotinstead of--watch - Profile startup: Measure import time
Related
- Hot Module Reloading - HMR API documentation
- Debugger - Debugging with watch mode
- React Fast Refresh - React HMR