# Fix "Class constructor MongoStore cannot be invoked without 'new'" After Upgrading connect-mongo

Yes. The agent moves the session store from connect-mongo 3's factory call to MongoStore.create, settles the mongodb peer range, and opens a pull request.

[Run this on Rush](https://cloud.getrush.ai/run/fix-class-constructor-mongostore-cannot-be-invoked-without-new/connect-mongo/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

`TypeError: Class constructor MongoStore cannot be invoked without 'new'` appears the first time an Express app starts after connect-mongo moves from 3 to 4. Version 3 exported a factory that took `express-session` and returned a store class; version 4 exports the class itself, so the old call `require("connect-mongo")(session)` tries to call a class and Node refuses. The run starts with that traceback, and the agent clones the repository into its own cloud sandbox.

In an Express app like 100devs/binary-upload-boom, `server.js` line 6 reads `const MongoStore = require("connect-mongo")(session);` and line 45 passes `store: new MongoStore({ mongooseConnection: mongoose.connection })` to `express-session`. `package.json` has `connect-mongo` at `^3.2.0`, `express-session` at `^1.17.1`, `mongoose` at `^5.12.3` and `mongodb` at `^3.6.5`; `npm start` runs `nodemon server.js` and the `Procfile` runs `node server.js`. `config/database.js` connects with `useNewUrlParser`, `useUnifiedTopology`, `useFindAndModify` and `useCreateIndex`, and the README expects a `.env` in `config/` with `PORT`, `DB_STRING` and the Cloudinary keys.

The agent reproduces the break in two steps. `npm install connect-mongo@4` is refused by npm with `ERESOLVE`, because connect-mongo 4.6.0 declares a peer dependency on `mongodb@^4.1.0` while the tree holds `mongodb@3.7.4` for mongoose 5. Installed with `--legacy-peer-deps`, `node server.js` throws the `TypeError` at `server.js:6:44` before any route is registered. It then reads what 4.6.0 exports: a class with a static `create`, no `mongooseConnection` option, and `mongoUrl`, `client` and `clientPromise` in its place.

The fix is three edits. Line 6 becomes `const MongoStore = require("connect-mongo");` and the store becomes `MongoStore.create({ mongoUrl: process.env.DB_STRING })`, or `MongoStore.create({ client: mongoose.connection.getClient() })` to reuse the mongoose connection, which mongoose 5.13 already exposes. To satisfy the peer range the agent moves `mongoose` to 6 and `mongodb` to 4, and then removes `useFindAndModify` and `useCreateIndex` from `config/database.js`, because mongoose 6 refuses them with `MongoParseError: options usefindandmodify, usecreateindex are not supported`. With a `DB_STRING` pointing at a local address the server prints its startup line, and the only other output is mongoose 6's `strictQuery` deprecation notice, which the pull request mentions but does not silence.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "After upgrading connect-mongo the server fails at startup with TypeError: Class constructor MongoStore cannot be invoked without 'new'. Reproduce it, port the express-session store to the connect-mongo 4 API (MongoStore.create with mongoUrl or the mongoose client), resolve the mongodb peer dependency by upgrading mongoose and mongodb together, remove connection options the new mongoose rejects, confirm node server.js starts, and open a pull request that lists each change and the version it is needed for.",
  "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

What comes back is a pull request that changes `server.js`, `config/database.js`, `package.json` and the lockfile. Its description quotes the `TypeError` with its file and column, the `ERESOLVE` message with the peer range, and the `MongoParseError` that the mongoose upgrade would otherwise introduce, so a reviewer sees why each of the three edits exists.

A reviewer checks that the store receives the same database as mongoose, that the session secret and cookie options are untouched, and that nothing else in the app used `mongooseConnection`. The pull request runs with the repository's checks; this app has no test script, so the evidence is the startup log kept in the session and the versions in the lockfile.

If the app cannot move mongoose to 6, the pull request offers the narrower route it also tried: keep mongoose 5, install connect-mongo 4 with `--legacy-peer-deps`, and pass the mongoose client to `MongoStore.create`, with the peer warning that implies. Which route lands is the reviewer's call; both are written down with their trade-off.

## Run it on every crashed deploy

Create an automation in the Project with this prompt and a `webhook` trigger, and copy the URL and secret Rush shows once. The platform that runs the app (a Heroku dyno from the `Procfile`, a container, a process manager) reports a crashed process to a handler on your server, which forwards the crash log to the trigger's URL with `Authorization: Bearer <secret>`. Each crash starts one run with the log after the prompt.

A dependency bump that never reaches production is cheaper to catch earlier: the same automation with a `github` trigger on `pull_request.labeled` and the label `dependencies` starts a run on each Dependabot pull request a maintainer labels (a label Dependabot sets itself comes from a bot and fires nothing), which installs the bumped version and reports whether `node server.js` still starts.

## Why run it on Rush?

The failing line runs before the first request, so no test suite would have caught it, and the peer conflict means the upgrade cannot even install cleanly on a laptop without a flag. A Rush run does the install in its own sandbox, keeps npm's refusal and Node's traceback, and ports the code against the exported API it reads from `node_modules`, not from memory.

Three packages move together here, and each has its own breaking change. The run tries the full upgrade and the narrow one, records what each produces, and opens one pull request with both written down. Several apps on the same session store can be handled at once, one run and one pull request each.

## Frequently asked questions

### Do I have to upgrade mongoose to use connect-mongo 4?

connect-mongo 4.6.0 asks for `mongodb@^4.1.0` as a peer, and mongoose 5 ships the mongodb 3 driver, so npm refuses the plain install. The agent's pull request upgrades mongoose and mongodb together; the alternative it documents is `--legacy-peer-deps` with the mongoose client passed to `MongoStore.create`.

### What replaces the mongooseConnection option?

connect-mongo 4 takes `mongoUrl`, `client` or `clientPromise`. The agent uses `mongoUrl: process.env.DB_STRING` by default, the same string `mongoose.connect` reads, so the store and the models share one database.

### Will existing sessions survive?

Sessions live in the `sessions` collection either way, and connect-mongo 4 keeps the same collection name by default. The pull request does not touch the session secret or cookie settings, so a signed cookie still matches its stored session.

### What about connect-mongo 5 or 6?

The same ported code works on 5. connect-mongo 6 ships ES modules, so the `require` line needs `.default` or an `import`; the agent pins the major the app's Node version and module system can take and says why in the pull request.

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

- [Fix "TypeError: CliRunner.__init__() got an unexpected keyword argument 'mix_stderr'" in Click 8.2](https://getrush.ai/run/fix-clirunner-mix-stderr-unexpected-keyword-argument/click/)
- [Fix Cloudflare Worker Errors From Its Logs](https://getrush.ai/run/fix-cloudflare-worker-errors/cloudflare-workers/)
- [Fix Errors in a Hono Deployment](https://getrush.ai/run/fix-cloudflare-worker-errors/hono/)
- [Fix TypeError: 'Collection' Object Is Not Callable in a PyMongo App](https://getrush.ai/run/fix-collection-object-is-not-callable/pymongo/)
- [Fix "ImportError: cannot import name 'Mapping' from 'collections'"](https://getrush.ai/run/fix-collections-abc-import-error/python/)
- [Fix "ArgumentError: Column expression, FROM clause, or other columns clause element expected" on SQLAlchemy 2](https://getrush.ai/run/fix-column-expression-or-from-clause-expected/sqlalchemy/)

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