Skip to main content
Bun’s bundler supports Hot Module Replacement (HMR) for fast development iteration without losing application state.

What is HMR?

Hot Module Replacement (HMR) allows you to update modules in a running application without a full page reload. When you edit a file:
  1. Bun detects the change
  2. Rebuilds only the affected modules
  3. Sends the update to the browser
  4. The browser applies the update without refreshing
This preserves:
  • Application state (form inputs, scroll position, etc.)
  • React component state
  • Global variables
  • WebSocket connections

Watch mode

Enable watch mode to rebuild automatically on file changes:
This watches all files imported by your entry point and triggers a rebuild when any of them change.

Development server

For full HMR support, use Bun’s development server (coming soon):
dev-server.ts

React Fast Refresh

React Fast Refresh is automatically enabled for .jsx and .tsx files:
Counter.tsx
When you edit this component:
  1. The component re-renders
  2. State is preserved (count value remains)
  3. No page reload occurs
Enable Fast Refresh explicitly:
Or via the JavaScript API:

Module types

Hot updates

Modules can opt into hot updates using the import.meta.hot API:

Self-accepting modules

A module can handle its own updates:
data.ts

Accepting dependencies

A module can handle updates to its dependencies:
app.ts

CSS HMR

CSS updates are applied without a page reload:
styles.css
When you change the background color:
styles.css
The style updates immediately in the browser without losing application state.

Image HMR

Images are also hot-reloaded:
When you replace logo.png, the new image appears without a page reload.

Configuration

Debouncing

Watch mode debounces file changes to avoid rebuilding too frequently:

Ignored paths

Exclude paths from watch mode:

Error handling

When a build error occurs:
  1. The error is displayed in the terminal
  2. The browser shows an error overlay (if using a dev server)
  3. The previous working code continues to run
  4. When you fix the error, HMR resumes

Performance

HMR in Bun is fast:
  • Incremental rebuilds (only changed modules)
  • Parallel processing
  • Minimal browser updates (only changed code)
  • No disk I/O for small changes

Examples

React application

App.tsx
index.tsx

Vanilla JavaScript

app.ts

State management

store.ts

Limitations

  • HMR requires a development server (static files don’t support HMR)
  • Not all changes can be hot-reloaded (e.g., changing module exports)
  • Some state might be lost (e.g., closures, module-level variables)
  • Full page reload is needed for:
    • HTML changes
    • Adding/removing files
    • Changes to build configuration
    • Changes to imported npm packages

Best practices

Keep state in React

Store application state in React components rather than module-level variables:

Use HMR API for cleanup

Clean up side effects when a module is replaced:

Organize for HMR

Split code into small modules that can be updated independently:

Troubleshooting

Changes not detected

Make sure the file is imported by your entry point:

Full reload on every change

Check if your modules are self-accepting:

State lost on update

Use React Fast Refresh for components or implement state preservation: