Koa Webpack Development Middleware
raw JSON →Koa Webpack is a development and hot module reloading middleware for Koa2 applications. It wraps and composes `webpack-dev-middleware` and `webpack-hot-client` into a single, cohesive middleware module, simplifying the integration of Webpack into a Koa server. Version 6.0.0 is the current stable release, which dropped support for Node.js 8. The package generally follows a maintenance release cadence with occasional minor updates, and major versions are released when significant breaking changes occur in its underlying dependencies (like `webpack-hot-client`) or Node.js compatibility is updated. A key differentiator is its ability to automatically use the `webpack` module and `webpack.config.js` from the project root, reducing boilerplate compared to other Koa Webpack integration solutions.
Common errors
error TypeError: app.use requires a generator function or middleware function ↓
const middleware = await koaWebpack(options); app.use(middleware); ensuring you await the Promise. error Error: Cannot find module 'webpack' ↓
npm install webpack or yarn add webpack. error TypeError: Cannot read properties of undefined (reading 'publicPath') (or similar for missing options) ↓
devMiddleware instead of dev and hotClient instead of hot. Warnings
breaking Node.js 8.x is no longer supported. Applications running on Node.js 8 will break or function incorrectly. ↓
breaking The `dev` and `hot` options were renamed to `devMiddleware` and `hotClient` respectively. ↓
breaking The `koaWebpack()` function now returns a Promise that resolves to the actual middleware, instead of returning the middleware directly. ↓
gotcha `koa-webpack` has a peer dependency on `webpack` version `^4.28.0` or higher. Failing to install this will result in runtime errors. ↓
gotcha Security vulnerabilities in underlying dependencies were patched in `v5.1.1` and `v5.2.3`. ↓
Install
npm install koa-webpack yarn add koa-webpack pnpm add koa-webpack Imports
- koaWebpack (ESM)
import koaWebpack from 'koa-webpack'; - koaWebpack (CommonJS) wrong
const middleware = require('koa-webpack')(options);correctconst koaWebpack = require('koa-webpack'); // ... later, inside an async function or IIFE: const middleware = await koaWebpack(options);
Quickstart
const Koa = require('koa');
const koaWebpack = require('koa-webpack');
const webpack = require('webpack');
const path = require('path');
const app = new Koa();
// A minimal webpack configuration for development
const webpackConfig = {
mode: 'development',
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
publicPath: '/' // Crucial for webpack-dev-middleware to serve assets
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: 'babel-loader' // Requires babel-loader, @babel/core, @babel/preset-env
}
]
}
};
const compiler = webpack(webpackConfig);
(async () => {
try {
const middleware = await koaWebpack({
compiler: compiler,
devMiddleware: {
publicPath: webpackConfig.output.publicPath,
stats: {
colors: true,
chunks: false,
'errors-only': true
},
writeToDisk: true // Example: Write assets to disk
},
hotClient: {
logLevel: 'warn', // Example: Suppress verbose hot-client logs
port: 8081 // Example: Specify a port for HMR WebSocket
}
});
app.use(middleware);
app.listen(3000, () => {
console.log('Koa server with webpack-dev-middleware and HMR listening on port 3000');
console.log('Open http://localhost:3000 in your browser.');
});
} catch (error) {
console.error('Failed to start koa-webpack middleware:', error);
}
})();