Webpack Dev Server
You'll learn how to set up webpack-dev-server to provide instant feedback during development with automatic reloading and hot module replacement.
What is Webpack Dev Server?
Think of webpack-dev-server as a watchful assistant that rebuilds your project and refreshes your browser every time you save a file. Instead of manually running webpack and hitting refresh after every code change, this development server automates the entire feedback loop.
Core Functionality
When you run webpack-dev-server, it starts a local web server (typically at http://localhost:8080) that serves your bundled application from memory—not from disk. This means faster rebuilds since nothing writes to actual files during development.
The server watches your source files. When you change a CSS rule, update a component, or modify any file webpack tracks, the server:
- Rebuilds only what changed
- Pushes updates to your browser
- Refreshes the page automatically (or updates modules without refresh using HMR)
Basic Setup
First, install it:
npm install --save-dev webpack-dev-server
Then configure it in your webpack.config.js:
module.exports = {
mode: 'development',
devServer: {
static: './dist',
hot: true,
port: 8080,
open: true
}
};
Run it with:
npx webpack serve
The hot: true option enables Hot Module Replacement (HMR)—modules update in the browser without a full page reload, preserving application state. The open: true option launches your browser automatically.
Key Takeaway: Webpack Dev Server creates a fast development environment with automatic rebuilding, live reloading, and hot module replacement, eliminating manual build-refresh cycles.