# Fix "'onBeforeSetupMiddleware' option is deprecated" from webpack-dev-server

Yes. An agent moves the dev server's custom routes to setupMiddlewares, starts the server, and calls each route before and after to show nothing changed.

[Run this on Rush](https://cloud.getrush.ai/run/fix-onbeforesetupmiddleware-option-is-deprecated/webpack-dev-server/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads the config that triggers the warning. In a front-end monitoring demo like miracle90/monitor, `webpack.config.js` has a `devServer` block whose `onBeforeSetupMiddleware: (devServer) => {...}` registers two routes on the dev server's Express app: `GET /success` answering `{ id: 1 }` and `POST /error` answering status 500. They exist for the demo page: `src/index.html` calls `fetch('/success')` and the history page under `src/monitor/history/2.network.html` fires an XHR at each, so the monitoring script under `src/monitor/lib` (`fetch.js`, `xhr.js`, `jsError.js`, `blankScreen.js`, `timing.js`, `longTask.js`, `pv.js`) has something to record. `package.json` runs the server with `"dev": "webpack-dev-server"` and declares `webpack-dev-server ^4.4.0`, `webpack ^5.62.1` and `webpack-cli ^4.9.1`.

Then it installs and reproduces, and the install is the first finding. `yarn.lock` pins webpack-dev-server 4.4.0 and webpack 5.62.1, but 224 of its entries resolve to `registry.nlark.com` and 57 to `registry.npmmirror.com`; the first host no longer resolves, so an install that honours the lock fails with `ENOTFOUND` on every one of those tarballs. Installed from `package.json` alone, the caret ranges give the newest 4.x webpack-dev-server and 5.x webpack, and `npx webpack-dev-server` prints `[DEP_WEBPACK_DEV_SERVER_ON_BEFORE_SETUP_MIDDLEWARE] DeprecationWarning: 'onBeforeSetupMiddleware' option is deprecated. Please use the 'setupMiddlewares' option.` before "compiled successfully". The agent curls both routes on the running server and records `{"id":1}` and a 500.

The fix keeps the routes and changes the hook. `setupMiddlewares: (middlewares, devServer) => { ... return middlewares; }` takes the array of middlewares the server is about to install, lets the function register its own routes on `devServer.app`, and must return the array; the two `app.get` and `app.post` lines and the guard that throws when `devServer` is missing move over unchanged. The agent restarts the server, confirms the warning is gone and both routes answer the same, and notes that webpack-dev-server 5 lists `setupMiddlewares` among its valid `devServer` options and not the old name, so the change is also what the next major needs.

It reports the lock file as its own decision. The config fix does not depend on it, but a lock that points at a dead registry is why nobody could reproduce the project's own pinned versions; the agent leaves the file alone, says why the install bypassed it, and lets the maintainer choose between regenerating it against the public registry and keeping it.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Our webpack dev server prints \"'onBeforeSetupMiddleware' option is deprecated. Please use the 'setupMiddlewares' option.\" Find every use of onBeforeSetupMiddleware and onAfterSetupMiddleware in our webpack config, say what each registers and which page or script depends on it, and move them to setupMiddlewares with the same routes. Start the dev server before and after, call every custom route, and open a pull request with the output. If the lock file cannot be installed, say why and install from package.json instead; do not change the lock file.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

A pull request that changes one block of `webpack.config.js`: the hook name, the `(middlewares, devServer)` signature and a `return middlewares` line, with the two routes intact. The description quotes the warning with its `DEP_WEBPACK_DEV_SERVER_ON_BEFORE_SETUP_MIDDLEWARE` code, shows `curl` output for `/success` and `/error` on the old and new config, names the installed webpack-dev-server and webpack versions, and explains why the lock file was bypassed.

A reviewer checks that both demo routes still answer, that the guard throwing on a missing `devServer` survived, and decides what to do about a `yarn.lock` whose entries point at a registry that no longer exists.

## Run it on every Dependabot pull request that bumps webpack-dev-server

The warning arrives with the minor bump and the error with the major, so the trigger is the pull request that bumps it. An automation with a `github` trigger on `pull_request.opened` and `pull_request.synchronize` runs this prompt on each one, with the event appended after the prompt, and pushes the `setupMiddlewares` change onto the bump's branch when the server warns or refuses the option.

For many front-end projects that still register dev routes the old way, your server sends this request once per repository, each with its own UUID as `request_id` so a retry returns the run already started, and reads each `run.completed` webhook; a key starts at most `max_concurrent` runs at once and queues the rest.

## Why run it on Rush?

A dev-server hook is only proven by starting the server and hitting the routes, which is exactly the step a one-line rename tempts people to skip. In a Rush sandbox the agent installs the project, starts the server on a port, calls each route before and after the change, and keeps the warning line and the curl output in the run.

The install itself turned up a second problem the warning hides: a lock file pinned to a registry that no longer answers. The agent separates the two, fixes the config, and gives the reviewer the facts about the lock instead of quietly regenerating it.

## Frequently asked questions

### What is different about setupMiddlewares?

It receives the server's middleware array as its first argument and the server as its second, and it has to return the array. Routes registered on `devServer.app` inside it work as before; the return value is what lets you also insert or reorder the server's own middlewares, which the old hooks could not.

### Does webpack-dev-server 5 still accept onBeforeSetupMiddleware?

The list of valid `devServer` options that version 5 prints when it refuses a config includes `setupMiddlewares` and not `onBeforeSetupMiddleware`, so the old name becomes a hard error there. Moving now means the next major upgrade does not start with a broken dev server.

### Why did the agent not install from the lock file?

Because the lock resolves most packages to `registry.nlark.com`, which does not resolve any more, so every install that honours it fails with `ENOTFOUND`. The agent installs from `package.json`, says so, and leaves the lock for you to regenerate against a registry you trust.

### Will the custom routes behave the same?

Yes. They are the same `app.get` and `app.post` calls on the same Express app, registered from the new hook. The pull request shows `/success` returning `{"id":1}` and `/error` returning a 500 on both the old and the new config.

## More webpack-dev-server jobs

- [Fix "Error: Cannot find module 'webpack-cli/bin/config-yargs'" When Starting webpack-dev-server](https://getrush.ai/run/fix-webpack-cli-config-yargs-not-found/webpack-dev-server/)
- [Fix "options has an unknown property 'contentBase'" in webpack-dev-server](https://getrush.ai/run/fix-dev-server-unknown-property-contentbase/webpack-dev-server/)

## More jobs like this: Fix a bug from an alert

- [Fix "You tried to access openai.ChatCompletion, but this is no longer supported in openai>=1.0.0"](https://getrush.ai/run/fix-openai-chatcompletion-no-longer-supported/openai/)
- [Fix "Option 'testPathPattern' was replaced by '--testPathPatterns'" in Jest 30](https://getrush.ai/run/fix-option-testpathpattern-was-replaced/jest/)
- [Fix "AttributeError: module 'os' has no attribute 'errno'" on Python 3.7 and Later](https://getrush.ai/run/fix-os-errno-attribute-error/os/)
- [Fix "Package subpath './lib/adapters/http' is not defined by \"exports\"" after upgrading axios](https://getrush.ai/run/fix-package-subpath-is-not-defined-by-exports/axios/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
