Koa Webpack Hot Middleware

raw JSON →
1.0.3 verified Thu Apr 23 auth: no javascript abandoned

koa-webpack-hot-middleware provides a Koa-specific adapter for `webpack-hot-middleware`, enabling hot module replacement (HMR) for Webpack bundles within a Koa application. It allows developers to integrate HMR directly into an existing Koa server setup, complementing `webpack-dev-middleware` rather than relying on `webpack-dev-server`. The provided npm metadata shows a current version of 1.0.3, with the latest activity (according to search results for `koa-webpack-middleware` which seems to be the successor or a related project) around 2017. Given the project's age, its dependencies on older Webpack plugin APIs (like `webpack.optimize.OccurenceOrderPlugin()`, which is deprecated), and the lack of recent releases, the package appears to be effectively abandoned. Its primary utility is for legacy Koa projects still using older Webpack configurations, as modern Koa and Webpack setups typically use newer alternatives like `koa-webpack`.

error TypeError: app.use is not a function
cause Attempting to use Koa middleware with an Express app, or an incorrect Koa app instance, or a very old Koa 1.x style `app` object that doesn't have the `.use` method for generator functions.
fix
Ensure you are using a valid Koa application instance. For Koa 2.x and above, ensure your Koa app is initialized correctly (e.g., const Koa = require('koa'); const app = new Koa();). If using Koa 1.x, verify your Koa version and middleware application syntax.
error Module not found: Error: Can't resolve 'webpack-hot-middleware/client' in '...' or similar client-side HMR errors.
cause The `webpack-hot-middleware/client` entry point was not correctly added to the webpack configuration, or `webpack-hot-middleware` itself was not installed.
fix
First, ensure webpack-hot-middleware is installed: npm install --save-dev webpack-hot-middleware. Then, verify that 'webpack-hot-middleware/client' (often with query parameters like ?path=/__webpack_hmr&timeout=20000) is included in the entry array or object of your webpack configuration for each bundle requiring HMR.
error TypeError: webpack.optimize.OccurenceOrderPlugin is not a function
cause Attempting to use `webpack.optimize.OccurenceOrderPlugin` with Webpack versions 4 or newer. This plugin was deprecated and removed, as its functionality became default.
fix
Remove new webpack.optimize.OccurenceOrderPlugin() from your webpack configuration's plugins array. For modern Webpack, it's not needed, and its absence won't affect functionality.
breaking `webpack.optimize.OccurenceOrderPlugin()` (misspelled 'Occurence') is deprecated and removed in Webpack 4+ and is no longer needed, as its functionality is enabled by default. Trying to use it with modern Webpack will result in a runtime error.
fix Remove `new webpack.optimize.OccurenceOrderPlugin()` from your Webpack plugins array. For modern Webpack versions, also replace `webpack.NoErrorsPlugin()` with `webpack.NoEmitOnErrorsPlugin()` or omit it as error handling has evolved.
gotcha This package appears to be largely unmaintained or abandoned, with the last releases dating back several years. It may have compatibility issues with recent versions of Node.js, Koa (especially Koa 2.x and later which introduced async/await), and modern Webpack (v4, v5, and v6).
fix For new projects or existing projects requiring active maintenance, consider using actively maintained alternatives such as `koa-webpack`, which bundles both development and hot module replacement middleware into a single module and supports modern Webpack and Koa versions.
gotcha The hot module replacement client (`webpack-hot-middleware/client`) must be explicitly added to your Webpack `entry` configuration. Failing to do so will prevent the client-side from connecting to the server and receiving HMR updates. This entry point often needs `?path=/__webpack_hmr` query parameters to correctly specify the event stream URL.
fix Ensure your Webpack configuration's `entry` object includes `'webpack-hot-middleware/client'` for each entry point that requires HMR. For example: `entry: { app: ['webpack-hot-middleware/client?path=/__webpack_hmr&timeout=20000', './src/index.js'] }`.
gotcha This middleware is designed for Koa 1.x or early Koa 2.x applications that primarily use `require()` for module imports and the generator-based middleware pattern, which can cause issues with modern Koa 2.x+ `async/await` middleware. While Koa 2.x can support legacy generator middleware via `koa-convert`, `koa-webpack-hot-middleware` itself does not directly expose an `async` function.
fix If using Koa 2.x or later, wrap `koa-webpack-hot-middleware` with `koa-convert` (e.g., `app.use(convert(koaWebpackHotMiddleware(compiler)))`) or migrate to a modern Koa webpack middleware that supports `async/await` natively, such as `koa-webpack`.
npm install koa-webpack-hot-middleware
yarn add koa-webpack-hot-middleware
pnpm add koa-webpack-hot-middleware

This quickstart demonstrates how to set up `koa-webpack-hot-middleware` in a basic Koa application, integrating it with `webpack-dev-middleware` to provide hot module replacement for client-side assets. It includes a minimal Webpack configuration with the necessary plugins and client-side entry point for HMR, then shows how to apply both middlewares to a Koa server.

const Koa = require('koa');
const webpack = require('webpack');
const webpackDevMiddleware = require('webpack-dev-middleware');
const koaWebpackHotMiddleware = require('koa-webpack-hot-middleware');
const path = require('path');

// A minimal webpack config for demonstration. Real apps would have more.
const webpackConfig = {
  mode: 'development',
  entry: {
    app: [
      'webpack-hot-middleware/client?path=/__webpack_hmr&timeout=20000',
      path.resolve(__dirname, 'client/index.js')
    ]
  },
  output: {
    path: path.resolve(__dirname, 'dist'),
    publicPath: '/',
    filename: '[name].bundle.js'
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoEmitOnErrorsPlugin() // Replaced NoErrorsPlugin
  ],
  devtool: 'inline-source-map'
};

const app = new Koa();
const compiler = webpack(webpackConfig);

app.use(webpackDevMiddleware(compiler, {
  noInfo: true,
  publicPath: webpackConfig.output.publicPath,
  stats: { colors: true }
}));

app.use(koaWebpackHotMiddleware(compiler));

// Serve your index.html or other static files
app.use(async (ctx, next) => {
  if (ctx.path === '/') {
    ctx.type = 'html';
    ctx.body = `
      <!DOCTYPE html>
      <html>
      <head><title>Koa HMR</title></head>
      <body>
        <div id="root"></div>
        <script src="/app.bundle.js"></script>
      </body>
      </html>
    `;
  } else {
    await next();
  }
});

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`Koa server listening on port ${PORT}`);
  console.log('Ensure you have a client/index.js file and run `npm install webpack webpack-cli webpack-dev-middleware webpack-hot-middleware koa koa-webpack-hot-middleware`');
});