Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 38 ยท Dev Server and HMR

Webpack Dev Server

Run webpack serve with webpack-dev-server: install and configure port, host, open, static, client overlay and compression, understand in-memory builds and how live reload works.

Running webpack after every edit and refreshing the browser gets old quickly. webpack-dev-server is a small development web server that builds your app, serves it from memory, watches your files, and tells the browser to reload as soon as something changes. It turns the edit-build-refresh loop into "save, and the page updates".

Terminal
npm install --save-dev webpack-dev-server

Start it with the serve command:

JSON
{
  "scripts": {
    "start": "webpack serve --mode development --open"
  }
}

Then npm start builds the project and prints something like Project is running at: http://localhost:8080/.

The devServer options

Configuration goes in the devServer section of the config file (The Webpack Configuration File):

JavaScript
module.exports = {
  mode: 'development',
  devServer: {
    port: 3000,
    host: 'localhost',
    open: true,
    hot: true,
    compress: true,
    static: { directory: path.resolve(__dirname, 'public') },
    client: {
      overlay: { errors: true, warnings: false },
      progress: true,
    },
    allowedHosts: 'auto',
  },
};
Option Purpose
port the port; 'auto' picks a free one
host localhost, or 0.0.0.0 to allow other devices on your network (phone testing)
open open a browser tab on start
hot enable Hot Module Replacement (default true)
liveReload full page reload when HMR cannot apply an update
static extra folders to serve as-is (images, robots.txt)
compress gzip responses
client.overlay show compile errors on top of the page
headers add HTTP headers
https / server serve over TLS
proxy forward API requests (Proxy and History Fallback)

Use host: '0.0.0.0' carefully: it exposes your development server to the local network.

It serves from memory

The dev server does not write your bundles to dist. It keeps them in memory (through webpack-dev-middleware), which is faster and leaves no stale files. The files are available at the URL given by output.publicPath (or the devMiddleware.publicPath option). That is why a dist folder can be empty while the site works.

The static option is separate: it serves real files from a folder, for assets that Webpack does not process. A request first tries the in-memory bundles, then static files. Remember that CopyWebpackPlugin is the production counterpart for those files.

How live reload works

The page opens a WebSocket back to the server. When Webpack finishes a rebuild the server sends a message (hash, ok), and the client script injected into your bundle either hot-swaps changed modules (HMR) or reloads the page.

sequenceDiagram
  participant E as Editor
  participant W as Webpack (watch)
  participant S as Dev server
  participant B as Browser
  E->>W: save file
  W->>S: new build in memory
  S->>B: "ok" message over WebSocket
  B->>S: request new bundle / hot update
  B->>B: apply module or reload page

The server pushes; the browser reacts.

This stand-in builds the same loop with plain Node: an HTTP server serving an "in-memory" bundle and sending reload events using Server-Sent Events (SSE), which are simpler than WebSockets for a demo.

Node.js Stand-in: a mini dev server with live reload

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Errors on screen

With client.overlay the dev server draws compile errors and runtime errors on top of the page, so you notice them even when the console is closed. Runtime errors you handle yourself do not trigger it. Turn off warnings in the overlay if a noisy dependency floods it (warnings: false), and use client.logging to set console verbosity.

Fast rebuilds: watch mode and caching

In development the dev server runs Webpack in watch mode: after the first build it only rebuilds what changed. To speed up cold starts too, enable the persistent cache:

JavaScript
cache: { type: 'filesystem' },

This stores build results in node_modules/.cache/webpack, so the second npm start is much faster. Delete that folder if you suspect stale results after changing loaders or Node versions.

The dev server is for development only. It is not hardened, it is not optimized, and it serves a development bundle. Deploy the files from a production build to a real web server or CDN.

Troubleshooting

  • Port already in use. Another dev server is running; change port or use port: 'auto'.
  • Changes do not appear. Check you are editing a file that is in the graph; on network drives or in containers file watching may need watchOptions: { poll: 1000 }.
  • Blank page at a deep URL like /users/5. A single-page app needs history fallback (Proxy and History Fallback).
  • "Invalid Host header". Set allowedHosts when accessing via a custom domain or tunnel.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where does webpack-dev-server keep the bundles it serves?

    The dev server uses an in-memory file system, so nothing needs to be written to dist.
  2. 2. What does devServer.static do?

    static serves a folder as-is, e.g. images or robots.txt.
  3. 3. How does the browser learn that a rebuild finished?

    The injected client keeps a WebSocket open and reacts to build messages.
  4. 4. Which value of host lets a phone on your network open the dev server?

    0.0.0.0 listens on all interfaces, which also exposes it to the local network.

Summary

  • webpack serve runs webpack-dev-server: it builds in memory, watches files and pushes updates to the browser.
  • Configure it in devServer: port, host, open, hot, static, client.overlay, proxy.
  • Updates reach the page over a WebSocket (HMR or a full reload).
  • Use cache: { type: 'filesystem' } for faster restarts.
  • It is a development tool; never deploy it.

Next: Hot Module Replacement