# Replace body-parser With express.json() in an Express App

Yes. An agent replaces the direct body-parser dependency with Express's built-in parsers and shows which parser version your app runs afterwards.

[Run this on Rush](https://cloud.getrush.ai/run/migrate-body-parser-to-express-json/body-parser/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The premise needs correcting before the work starts: `body-parser` is not deprecated. It is published and maintained, and Express itself depends on it. What changed is that Express 4.16 began exporting the same parsers as `express.json()` and `express.urlencoded()`, so an app no longer has to declare the package itself. The agent states this in the pull request, because it decides what the change can and cannot achieve. In a small service like MelwinKfr/gitlab-webhook-translator, `app.js` requires `body-parser` on line 3 and mounts `bodyParser.json()` and `bodyParser.urlencoded({ extended: true })`. `package.json` declares `"body-parser": "^1.17.1"` next to `"express": "^4.15.2"`.

That Express range is the first problem. It admits 4.15, and on Express 4.15 reading `express.json` throws `Most middleware (like json) is no longer bundled with Express and must be installed separately`. The agent raises the range to `^4.16.0` in the same change, so the code and the manifest agree. It then replaces the two calls, keeps `extended: true` exactly as written, and deletes the `require` and the dependency line.

The second problem is in the lock file, and it is the reason the agent prints a dependency tree before and after. With this repository's `package-lock.json`, `npm ci` installs two copies of the parser: `body-parser` 1.18.3 at the top level, which the app uses today, and 1.18.2 nested under `express` 4.16.3, which pins it exactly. The two are different function objects; `express.json === bodyParser.json` is false. Removing the direct dependency therefore moves the app from 1.18.3 to the older 1.18.2 unless Express is updated too. The agent updates Express within the 4.x line, shows the single parser copy that results, and runs `npm audit` before and after, since the old lock's `body-parser` and `qs` both carry advisories.

To show behaviour is unchanged it sends the same requests through both setups on a local port: a JSON object, a JSON body with a charset, malformed JSON, a bare JSON string, a nested urlencoded form, JSON sent as `text/plain`, an empty POST and a JSON body over 100 KB. It records status and parsed body for each. This service listens on port 80 and reads `/translations.json`, so the agent tests the middleware in a small harness and says it did not start `app.js` itself. There is no Docker in the sandbox, and it does not build the image; it reads the `Dockerfile` and reports what it sees there.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Replace this Express app's direct use of the body-parser package with Express's built-in express.json() and express.urlencoded(). Start by stating whether body-parser is actually deprecated, from the npm registry, and what this change does and does not remove. Read package.json, the lock file, the Dockerfile and every file that requires body-parser. List each parser mounted and its options: json, urlencoded with its extended flag, raw, text, limit, type, verify. Check the declared Express range: if it admits a version older than 4.16.0, show what reading express.json does on such a version and raise the range. Install from the lock file and print npm ls for express, body-parser and qs. Report whether the app's body-parser and the copy Express uses are the same version and the same function object. Before editing, write a harness that mounts the parsers the way the app does and sends a JSON object, malformed JSON, a nested urlencoded form, JSON with a text/plain content type, an empty POST and a JSON body larger than the limit, recording status and parsed body. Make the change, keeping every option as written. If the swap would move the app to an older parser than it uses today, update Express within its current major so it does not. Remove body-parser from package.json, update the lock, print npm ls again, and rerun the harness. Run npm audit before and after. Do not start a server on a privileged port, do not build Docker images, and do not change handler code. Open a pull request with both trees and both harness outputs.",
  "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 edits `app.js`, `package.json` and `package-lock.json`. The description shows `npm ls express body-parser qs` before, with two parser copies, and after, with one under Express. It includes the request table from the harness with a status and body per row for both setups, and the `npm audit` summary before and after. It says in its first lines that `body-parser` remains installed as a dependency of Express; the direct declaration is what was removed.

The reviewer reads three findings the agent reports and does not fix. The default body limit is 100 KB on either parser, so a webhook delivery larger than that is answered 413 before any handler runs. The `Dockerfile` starts from `node:7-alpine` and copies only `package.json` before `RUN npm install`, so the image never uses the lock file and resolves whatever the ranges allow on the day it is built. And `req.body` is an empty object when a request arrives with a content type neither parser handles, which is what `Translation.process` receives in that case. Each is a line in the description with the file it comes from.

## Run it on every failed audit workflow

A scheduled GitHub Actions workflow that runs `npm audit --audit-level=high` turns stale parser and query-string packages into a red check. Add an automation to the Project with a `github` trigger on `workflow_run.failure`, and save a prompt that begins with the text above and adds one sentence: the failed workflow run follows this prompt, so read which packages it names. Rush places the event after the prompt as third-party data and the prompt stays the only instruction.

Workflow runs from forks never fire the trigger. If the same workflow fails every night until the fix merges, each failure is an event, so set the automation's max runs per hour low and merge or close the first pull request before the next one opens.

## Why run it on Rush?

Whether this change is a cleanup or a silent downgrade depends on what the lock file resolves, and that is only knowable by installing it. The run does a clean `npm ci` in its own sandbox, prints the tree, makes the change and prints it again. A developer's `node_modules`, shaped by months of installs, would hide the two-copy state that the lock describes.

The request table is the other half. Parsers differ in their edges: what counts as JSON, what an oversized body gets, what an unmatched content type leaves in `req.body`. The harness asks both setups the same questions and the answers are kept with the session, so the claim that nothing changed for clients is backed by rows a reviewer can read.

## Frequently asked questions

### Is body-parser deprecated?

No. The package is maintained and Express depends on it. The strike-through many editors show is on calling `bodyParser()` as a function with no parser chosen, which was deprecated long ago; `bodyParser.json()` and `bodyParser.urlencoded()` are fine. Since Express 4.16 the same parsers are available as `express.json()` and `express.urlencoded()`.

### Is express.json() exactly the same as bodyParser.json()?

It is the `json` function from the `body-parser` copy that Express depends on. If your app also declares `body-parser`, npm may install a second copy at another version, and then the two functions are different builds of the same code. The agent prints both versions so you know which one you move to.

### Do I still need the extended option?

Keep it as it is. `extended: true` parses nested keys such as `a[b]=1` with the `qs` library, and `false` uses the simpler built-in parser. On Express 4, leaving the option out logs a deprecation warning, so the agent copies your current value across unchanged.

### Will this fix PayloadTooLargeError?

No. Both parsers default to a 100 KB limit and answer a larger body with status 413. Raise it with an option, for example `express.json({ limit: '1mb' })`, sized to the largest request you expect. The agent reports the current limit and leaves it alone, because raising it is a decision about what your server accepts.

## More body-parser jobs

- [Fix the body-parser "provide extended option" Deprecation Warning in Express](https://getrush.ai/run/fix-body-parser-extended-option-deprecation/body-parser/)

## More jobs like this: Upgrade dependencies weekly

- [Migrate From the Archived github.com/boltdb/bolt to go.etcd.io/bbolt in a Go Service](https://getrush.ai/run/migrate-boltdb-to-bbolt/boltdb/)
- [Migrate From boto to boto3 in a Python Service](https://getrush.ai/run/migrate-boto-to-boto3/boto/)
- [Convert a CoffeeScript Project to Modern JavaScript With decaffeinate](https://getrush.ai/run/migrate-coffeescript-to-javascript/coffeescript/)
- [Migrate a Go Project From dep (Gopkg.toml) to Go Modules](https://getrush.ai/run/migrate-dep-to-go-modules/dep/)
- [Migrate From drf-yasg to drf-spectacular in Django REST Framework](https://getrush.ai/run/migrate-drf-yasg-to-drf-spectacular/drf-yasg/)

## 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/)
