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.
index.ts
index.ts
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
import.meta.hot.accept()
Theaccept() method indicates that a module can be hot-replaced. Called without arguments, it means Bun can replace this module by re-evaluating the file. After a hot update, Bun automatically patches the module’s importers.
index.ts
index.ts imports. Whenever you save foo.ts or any of its dependencies, the update bubbles up to index.ts, which re-evaluates. Bun then patches the files that import index.ts to import the new version of getNegativeCount(). If you update only index.ts, Bun re-evaluates only that one file, 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. 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.
index.ts
Accepting other modules
index.ts
With multiple dependencies
index.ts
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()).
index.tsx
data as {}, so you cannot use it as a state holder.
import.meta.hot.dispose()
Attaches an on-dispose callback. Bun calls it:- 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())
index.ts
import.meta.hot.prune()
Attaches an on-prune callback. Bun calls it 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:
index.ts
If you used
dispose instead, the WebSocket would close and re-open on every hot update. Both versions of the code
prevent page reloads when you update imported files.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.
index.ts
Built-in events
For compatibility with Vite, these events are also available with the
vite:* prefix instead of bun:*.