Skip to main content

Setting up webpack dev middleware in express

11/7/20153 min read


What is webpack dev server?

Webpack dev server is a live reloading server for webpack.

What is webpack dev middleware?

It's a simple wrapper middleware for webpack. It serves the files emitted from webpack over a connect server.

It has a few advantages over bundling it as files:

  • No files are written to disk, it handles the files in memory
  • If files changed in watch mode, the middleware no longer serves the old bundle, but delays requests until the compiling has finished. You don't have to wait before refreshing the page after a file modification.

What is webpack hot middleware?

Webpack hot reloading using only webpack-dev-middleware. This allows you to add hot reloading into an existing server without webpack-dev-server.

Using webpack-dev-server as a middleware

Web pack provides an express middleware that you can plug into your app to serve up your fronted assets via web pack-dev-server rather than express.static or express/serve-static.
To do this you’ll need to install webpack-dev-middleware and webpack-hot-middleware

    npm i webpack-dev-middleware webpack-hot-middleware

First off you need to make a file. In your config, add webpack/hot/dev-server and webpack-hot-middleware/client to your entry point as well as your js or coffee script frontend entry point.

entry:    [

Make your output path ‘/‘ because remember, you’re building your app into memory now rather than a build folder.

output: {
    slug: '/'
    publicslug: 'https://localhost:3000/scripts/'
    filename: 'bundle.js'

Add in whatever loaders and plugins you need and target 'web' … you can see my config [here(

Now we’ve got our dev config, we need to build our express application.
We need to require webpack, webpack-dev-middleware and webpack-hot-middleware as well as express.

express = require 'express'
webpack = require 'webpack'
webpackDevMiddleware = require 'webpack-dev-middleware'
webpackHotMiddleware = require 'webpack-hot-middleware'

We need to create our express app as usual and a router to receive our requests.

app = express()
router = express.Router()

router.get('/', someController)

Now for the webpack magic, we need to import our webpack config

config = require './config/'

Next, we need to create a compiler by feeding our config into webpack.

compiler = webpack(config)

Now we need to hook in the webpack-dev-middleware

app.use(webpackDevMiddleware(compiler, {
    publicslug: config.output.publicPath,
    stats: {colors: true}

The public path here is where you want your front-end bundle to end up.
We also need to plug in the webpack-hot-middleware

app.use(webpackHotMiddleware(compiler, {
    log: console.log

When all this is done, call listen on the app.

app.listen(3000,  -> console.log 'listening on 3000')

Now we just need to include the bundle.js in your index.html and we’re off.
I use nodemon to monitor my back end files so I can make changes and restart the node process, but remember to exclude your client-side files so webpack-dev-middleware can take care of them instead.

When you load up the app, you should see webpack-hot-middleware trying to connect to the dev server in the console


How do you hook this into production code?

Simple, have a conditional statement checking your environment variable, if you’re in a dev environment use the webpack-dev-middleware, if not, use express.static to serve your static assets.

You can checkout my boilerplate express app with webpack-dev-middleware and webpack-hot-middleware plugged in at

For your convenience

I've written a small packackage which encapsulates webpack dev middleware and hot reloader wrapped in express. You can find it here

© 2021 by Madole.
GitHub Repository
Last build: 1662286611335