Bun Hot Reload
Bun provides efficient Hot Reload and Watch Mode, significantly improving development efficiency. This chapter introduces Bun's hot reload functionality.
Watch Mode
Basic Usage
Use the --watch flag to listen for file changes and automatically rerun:
How It Works
Files Watched
Bun automatically watches:
- Entry files
- All imported modules
- Related config files
Hot Mode
Hot Reload (Without Restarting Process)
Use the --hot flag to enable true hot reload:
Hot Reload vs Watch Mode
HTTP Server Hot Reload
Run:
After modifying code, the server will hot update without restarting, and existing connections are maintained.
Configuring Watch
Additional Watched Files
package.json Scripts
State Preservation
Hot Reload State
Exporting Default
Practical Applications
Development Server
Run:
Development Tools Integration
Testing Watch Mode
Watch Tests
Tests will automatically rerun when test files change.
Run Related Tests Only
Custom Watch Logic
Using fs.watch
Debounce Processing
Production Environment Notes
Do Not Use in Production
Environment Separation
FAQ
Hot Reload Not Working
State Lost
Some Files Not Being Watched
Summary
This chapter covered:
- ✅ Watch mode (
--watch) automatic restart - ✅ Hot mode (
--hot) no-restart hot update - ✅ State preservation techniques
- ✅ Development server configuration
- ✅ Testing watch mode
- ✅ Custom watch logic
Next Steps
Continue reading SQLite Database to learn about Bun's built-in database support.