HotModuleReplacementPlugin enables Hot Module Replacement (HMR), which allows modules to be updated at runtime without requiring a full page refresh.
Usage
When using webpack-dev-server, HMR is enabled automatically. You don’t need to add this plugin explicitly.
How It Works
HMR exchanges, adds, or removes modules while an application is running without a full reload. This significantly speeds up development by:- Retaining application state that would be lost on full reload
- Saving time by only updating changed modules
- Instantly reflecting changes in the browser
Module API
module.hot.accept
Accept updates for a module and its dependencies:Module path(s) to watch for updates
Function called when modules are updated
module.hot.decline
Mark a module as not updatable:import.meta.webpackHot
ESM syntax for HMR:Examples
Basic Setup
With Callback
Multiple Dependencies
React Hot Reload
Framework Support
Many frameworks have built-in HMR support:
- React: Use
react-refresh-webpack-plugin - Vue: Built into
vue-loader - Angular: Supported via
@angularclass/hmr
Troubleshooting
Full Reload Occurs
If HMR falls back to a full reload:- Check that modules are properly accepting updates
- Verify no syntax errors in the updated modules
- Ensure dependencies are correctly specified