Baike.dev
All toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
W

webpack-dev-middleware

> 编程语言
Open source

A development middleware for webpack

2.5K stars0 likes2 views
WebsiteGitHub

About

A development middleware for webpack

[![npm][npm]][npm-url] [![node][node]][node-url] [![tests][tests]][tests-url] [![coverage][cover]][cover-url] [![discussion][discussion]][discussion-url] [![size][size]][size-url]

webpack-dev-middleware

An express-style development middleware for use with webpack bundles and allows for serving of the files emitted from webpack. This should be used for development only.

Some of the benefits of using this middleware include:

  • No files are written to disk, rather it handles files in memory
  • If files changed in watch mode, the middleware delays requests until compiling has completed.
  • Supports hot module reload (HMR).

Getting Started

First thing's first, install the module:

bash
npm install webpack-dev-middleware --save-dev

[!WARNING]

We do not recommend installing this module globally.

Usage

javascript
const express = require("express");
const webpack = require("webpack");
const middleware = require("webpack-dev-middleware");

const compiler = webpack({
  // webpack options
});

const app = express();

app.use(
  middleware(compiler, {
    // webpack-dev-middleware options
  }),
);

app.listen(3000, () => console.log("Example app listening on port 3000!"));

See below for an example of use with fastify.

Options

Name Type Default Description
methods Array [ 'GET', 'HEAD' ] Allows to pass the list of HTTP request methods accepted by the middleware
headers Array|Object|Function undefined Allows to pass custom HTTP headers on each request.
index boolean|string index.html If false (but not undefined), the server will not respond to requests to the root URL.
mimeTypes Object undefined Allows to register custom mime types or extension mappings.
mimeTypeDefault string undefined Allows to register a default mime type when we can't determine the content type.
etag boolean| "weak"| "strong" undefined Enable or disable etag generation.
lastModified boolean undefined Enable or disable Last-Modified header. Uses the file system's last modified value.
cacheControl boolean|number|string|Object undefined Enable or disable setting Cache-Control response header.
cacheImmutable boolean undefined Enable or disable setting Cache-Control: public, max-age=31536000, immutable response header for immutable assets.
publicPath string undefined The public path that the middleware is bound to.
stats boolean|string|Object stats (from a configuration) Stats options object or preset name.
serverSideRender boolean undefined Instructs the module to enable or disable the server-side rendering mode.
writeToDisk boolean|Function false Instructs the module to write files to the configured location on disk as specified in your webpack configuration.
outputFileSystem Object memfs Set the default file system which will be used by webpack as primary destination of generated files.
modifyResponseData Function undefined Allows to set up a callback to change the response data.
hot boolean|Object false Enables a Server-Sent Events endpoint that drives the browser HMR client.
forwardError boolean false Enable or disable forwarding errors to the next middleware.

The middleware accepts an options Object. The following is a property reference for the Object.

methods

Type: Array
Default: [ 'GET', 'HEAD' ]

This property allows a user to pass the list of HTTP request methods accepted by the middleware**.

headers

Type: Array|Object|Function Default: undefined

This property allows a user to pass custom HTTP headers on each request. eg. { "X-Custom-Header": "yes" }

or

javascript
webpackDevMiddleware(compiler, {
  headers: () => ({
    "Last-Modified": new Date(),
  }),
});

or

javascript
webpackDevMiddleware(compiler, {
  headers: (req, res, context) => {
    res.setHeader("Last-Modified", new Date());
  },
});

or

javascript
webpackDevMiddleware(compiler, {
  headers: [
    {
      key: "X-custom-header",
      value: "foo",
    },
    {
      key: "Y-custom-header",
      value: "bar",
    },
  ],
});

or

javascript
webpackDevMiddleware(compiler, {
  headers: () => [
    {
      key: "X-custom-header",
      value: "foo",
    },
    {
      key: "Y-custom-header",
      value: "bar",
    },
  ],
});

index

Type: Boolean|String Default: index.html

If false (but not undefined), the server will not respond to requests to the root URL.

mimeTypes

Type: Object
Default: undefined

This property allows a user to register custom mime types or extension mappings. eg. mimeTypes: { phtml: 'text/html' }.

Please see the documentation for mime-types for more information.

mimeTypeDefault

Type: String
Default: undefined

This property allows a user to register a default mime type when we can't determine the content type.

etag

Type: "weak" | "strong"
Default: undefined

Enable or disable etag generation. Boolean value use

lastModified

Type: Boolean Default: undefined

Enable or disable Last-Modified header. Uses the file system's last modified value.

cacheControl

Type: Boolean | Number | String | { maxAge?: number, immutable?: boolean } Default: undefined

Depending on the setting, the following headers will be generated:

  • Boolean - Cache-Control: public, max-age=31536000000
  • Number - Cache-Control: public, max-age=YOUR_NUMBER
  • String - Cache-Control: YOUR_STRING
  • { maxAge?: number, immutable?: boolean } - Cache-Control: public, max-age=YOUR_MAX_AGE_or_31536000000, also , immutable can be added if you set the immutable option to true

Enable or disable setting Cache-Control response header.

cacheImmutable

Type: Boolean Default: undefined

Enable or disable setting Cache-Control: public, max-age=31536000, immutable response header for immutable assets (i.e. asset with a hash like image.a4c12bde.jpg). Immutable assets are assets that have their hash in the file name therefore they can be cached, because if you change their contents the file name will be changed. Take preference over the cacheControl option if the asset was defined as immutable.

publicPath

Type: String Default: output.publicPath (from a configuration)

The public path that the middleware is bound to.

Best Practice: use the same publicPath defined in your webpack config. For more information about publicPath, please see the webpack documentation.

stats

Type: Boolean|String|Object Default: stats (from a configuration)

Stats options object or preset name.

serverSideRender

Type: Boolean
Default: undefined

Instructs the module to enable or disable the server-side rendering mode. Please see Server-Side Rendering for more information.

writeToDisk

Type: Boolean|Function
Default: false

If true, the option will instruct the module to write files to the configured location on disk as specified in your webpack config file. Setting writeToDisk: true won't change the behavior of the webpack-dev-middleware, and bundle files accessed through the browser will still be served from memory. This option provides the same capabilities as the WriteFilePlugin.

This option also accepts a Function value, which can be used to filter which files are written to disk. The function follows the same premise as Array#filter in which a return value of false will not write the file, and a return value of true will write the file to disk. eg.

javascript
const webpack = require("webpack");

const configuration = {/* Webpack configuration */};
const compiler = webpack(configuration);

middleware(compiler, {
  writeToDisk: (filePath) => /superman\.css$/.test(filePath),
});

outputFileSystem

Type: Object
Default: memfs

Set the default file system which will be used by webpack as primary destination of generated files. This option isn't affected by the writeToDisk option.

You have to provide .join() and mkdirp method to the outputFileSystem instance manually for compatibility with webpack@4.

This can be done simply by using path.join:

javascript
const path = require("node:path");
const mkdirp = require("mkdirp");
const myOutputFileSystem = require("my-fs");
const webpack = require("webpack");

myOutputFileSystem.join = path.join.bind(path); // no need to bind
myOutputFileSystem.mkdirp = mkdirp.bind(mkdirp); // no need to bind

const compiler = webpack({/* Webpack configuration */});

middleware(compiler, { outputFileSystem: myOutputFileSystem });

modifyResponseData

Allows to set up a callback to change the response data.

javascript
const webpack = require("webpack");

const configuration = {/* Webpack configuration

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

JavaScriptmiddlewarewebpackwebpack-dev-middleware

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言