Course contentsShow
← JavaScript
Lesson 2450 of 2,72350. JavaScript Build ToolingFree lesson

Hot Module Replacement

How bundlers enable updating modules in a running app without full page reloads, preserving application state.

Hot Module Replacement

What you'll learn: How bundlers like Webpack enable you to update code in a running app without losing application state or triggering a full page reload.

The Problem HMR Solves

During development, every code change typically forces a full browser refresh. That means re-initializing your app from scratch, losing form inputs, UI state, scroll positions, and any runtime data you've built up. When you're tweaking a feature deep in your application, this becomes painfully slow.

How Hot Module Replacement Works

Hot Module Replacement (HMR) is a bundler feature that intercepts module updates and swaps them into your running application. Think of it like changing a tire while the car is still moving—the engine keeps running, but you've replaced a component.

Here's the flow:

  1. Your bundler (Webpack, Vite, etc.) watches your source files
  2. When you save a change, it rebuilds only the affected module
  3. The dev server pushes this updated module to the browser via WebSocket
  4. Your app's HMR runtime replaces the old module with the new one
  5. Framework integrations (React Fast Refresh, Vue HMR) preserve component state

The Developer Experience Win

Instead of reloading and navigating back to where you were testing, you see your code changes applied instantly while your app stays exactly where it was. You keep your authenticated session, your modal stays open, your form data persists.

Not all modules can hot-reload safely—some changes (like modifying global state or core infrastructure) still require full refreshes. But for UI components and most application code, HMR dramatically accelerates the feedback loop.

Key Takeaway: Hot Module Replacement lets bundlers inject updated modules into a running app without full reloads, preserving state and making development dramatically faster by eliminating the navigate-back-to-where-you-were cycle.