HMR is enabled by default when using Bun’s full-stack development server.
import.meta.hot API Reference
Bun implements a client-side HMR API modeled after Vite’s import.meta.hot API. You can check for it with if (import.meta.hot), which tree-shakes it in production.
The HMR API is still a work in progress. Some features are missing. To disable HMR in
Bun.serve, set the development option to { hmr: false }.API Methods
| Method | Status | Notes |
|---|---|---|
hot.accept() | ✅ | Indicate that a hot update can be replaced gracefully. |
hot.data | ✅ | Persist data between module evaluations. |
hot.dispose() | ✅ | Add a callback function to run when a module is about to be replaced. |
hot.invalidate() | ❌ | |
hot.on() | ✅ | Attach an event listener. |
hot.off() | ✅ | Remove an event listener from on. |
hot.send() | ❌ | |
hot.prune() | 🚧 | Callback is currently never called. |
hot.decline() | ✅ | No-op to match Vite’s import.meta.hot. |
import.meta.hot.accept()
Theaccept() method indicates that a module can be hot-replaced. Called without arguments, it means this module can be replaced by re-evaluating the file. After a hot update, Bun automatically patches the module’s importers.
index.ts imports. Whenever foo.ts or any of its dependencies are saved, the update bubbles up to index.ts, which re-evaluates. Files that import index.ts are then patched to import the new version of getNegativeCount(). If only index.ts is updated, only that one file is re-evaluated, and the counter in foo.ts is reused.
Combine this with import.meta.hot.data to transfer state from the previous module to the new one.
When no modules call
import.meta.hot.accept() (and there isn’t React Fast Refresh or a plugin calling it for you),
the page reloads when the file updates, and a console warning shows which files were invalidated. This warning is safe
to ignore if it makes more sense to rely on full page reloads.With callback
When passed a callback,import.meta.hot.accept works as it does in Vite. Instead of patching the importers of this module, it calls the callback with the new module.
Accepting other modules
With multiple dependencies
undefined for any that had errors.
import.meta.hot.data
import.meta.hot.data carries state from the previous version of a module to the new one across a hot replacement. Writing to import.meta.hot.data also marks the module as self-accepting (equivalent to calling import.meta.hot.accept()).
data is inlined to be {}, meaning it cannot be used as a state holder.
import.meta.hot.dispose()
Attaches an on-dispose callback. This is called:- Just before the module is replaced with another copy (before the next is loaded)
- After the module is detached (removing all imports to this module, see
import.meta.hot.prune())
import.meta.hot.prune()
Attaches an on-prune callback. This is called when all imports to this module are removed, but the module was previously loaded. Use it to clean up resources that were created when the module was loaded. Unlikeimport.meta.hot.dispose(), it pairs better with accept and data for managing stateful resources. A full example managing a WebSocket:
If
dispose was used instead, the WebSocket would close and re-open on every hot update. Both versions of the code
prevent page reloads when imported files are updated.import.meta.hot.on() and off()
Useon() and off() to listen for events from the HMR runtime. Event names carry a prefix so that plugins do not conflict with each other.
Built-in events
| Event | Emitted when |
|---|---|
bun:beforeUpdate | before a hot update is applied. |
bun:afterUpdate | after a hot update is applied. |
bun:beforeFullReload | before a full page reload happens. |
bun:beforePrune | before prune callbacks are called. |
bun:invalidate | when a module is invalidated with import.meta.hot.invalidate(). |
bun:error | when a build or runtime error occurs. |
bun:ws:disconnect | when the HMR WebSocket connection is lost. This can indicate the development server is offline. |
bun:ws:connect | when the HMR WebSocket connects or re-connects. |
For compatibility with Vite, these events are also available with the
vite:* prefix instead of bun:*.