Upgrading to Express 5

Express 5 has been 'coming soon' for about ten years. In September it finally shipped as 5. 0. 0. The good news is that most apps need only small changes.

Express 5 has been “coming soon” for about ten years. In September it finally shipped as 5.0.0. The good news is that most apps need only small changes. The bad news is that one of those changes, the route syntax, will stop your app from starting until you fix it.

Express 5 needs Node 18 or later.

The best part: async errors just work

In Express 4, a rejected promise in a route handler wasn’t passed to your error handler. The request hung, or the process logged an unhandled rejection, unless you wrapped every handler in a try/catch or a helper like express-async-handler.

In Express 5 this works on its own:

app.get('/users/:id', async (req, res) => {
  const user = await db.users.find(req.params.id); // throws: "db down"
  res.json(user);
});

app.use((err, req, res, next) => {
  console.error(err);
  res.status(500).json({ error: 'Something went wrong' });
});
GET /users/1 → 500 {"error":"Something went wrong"}

If a handler or middleware returns a promise that rejects, Express calls next(err) for you. You can delete all those wrappers.

Route paths: the breaking change

Express 5 uses a new version of path-to-regexp, and a few patterns that were common in Express 4 now throw a TypeError when the route is registered. That means at startup, which is good, because you can’t miss it.

Wildcards need a name. * on its own is no longer valid:

// Express 4
app.get('/files/*', handler);

// Express 5
app.get('/files/*path', handler);

The matched part comes back as an array of segments:

app.get('/files/*path', (req, res) => {
  res.json(req.params);
});
// GET /files/a/b/c.txt → {"path":["a","b","c.txt"]}

A catch-all like app.get(’*’, …) for a single-page app becomes app.get(’/*splat’, …). Note that this doesn’t match the root path / itself. If you need both, use /{*splat}.

Optional parameters use braces. ? is gone:

// Express 4
app.get('/users/:id?', handler);

// Express 5
app.get('/users{/:id}', handler);
// GET /users   → req.params.id is undefined
// GET /users/7 → req.params.id is '7'

No regex inside strings. Something like /api/:version(v1|v2) doesn’t work anymore. Either use a real RegExp for the route, or match the plain parameter and validate it in the handler.

A quick grep for ’*, ?’ and ( in your route definitions finds most of these.

req.body is undefined without a parser

In Express 4, req.body was {} whenever a body parser was mounted, even if the request had no body or a different content type. In Express 5 it’s undefined. Code like const { name } = req.body now throws a TypeError instead of quietly giving you undefined.

This shows up with requests that have no body, webhooks that send a content type your parser doesn’t handle, and clients that forget the Content-Type header. Default it where it matters:

const { name } = req.body ?? {};

Query strings are simpler

The default query parser no longer builds nested objects:

GET /orders?filter[status]=open&tag=a&tag=b

Express 4: { filter: { status: 'open' }, tag: ['a', 'b'] }
Express 5: { 'filter[status]': 'open', tag: ['a', 'b'] }

Repeated keys still become arrays. If your API relies on the bracket syntax, switch back to the old behaviour with app.set(‘query parser’, ‘extended’).

Removed methods

These were deprecated for years and are now gone. Most codebases have a few:

Express 4 Express 5
app.del() app.delete()
req.param(‘id’) req.params.id, req.body.id or req.query.id
res.send(404) res.sendStatus(404)
res.json(obj, 201) res.status(201).json(obj)
res.sendfile() res.sendFile()
res.redirect(‘back’) res.redirect(req.get(‘Referrer’) || ’/’)

res.status() is also stricter now. It only accepts integers between 100 and 999, so res.status(‘200’) throws.

How to upgrade

  • Upgrade to Node 18 or later, then install express@5.
  • Start the app. Fix every route that throws at startup.
  • Search for the removed methods in the table above.
  • Check routes that read req.body without a body parser, and anything that relies on nested query objects.
  • Delete your async wrappers last, once everything else passes.

The upgrade itself is usually an afternoon of work. Removing the try/catch boilerplate from every async handler is the part you’ll be glad you did.