Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Express warns that res.send(status) is deprecated

Errorexpress deprecated res.send(status): Use res.sendStatus(status) instead

Short answer

Express 4 prints this warning when a number is passed to res.send() as a status code. Express 5 removed that signature. Replace res.send(404) with res.sendStatus(404), or res.status(404).send('text') when you want your own body. If the number is meant as the body, quote it. Find the call site with node --trace-deprecation.

  • Applies to Express 4.21, Express 5, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Run the app with Node's trace flag. The warning then prints the full stack, and its first lines name the file and line that made the call. By default it shows only the location of the call, once per call site.

    node --trace-deprecation server.js
  2. 2Match what you see, then jump to its fix

    1. 1The warning text itself, and the stack points at a line like res.send(404)A status number is passed to res.send()Fix
    2. 2The call is res.send(count) or res.send(42), and the client expected the number back as dataA number was meant to be the response bodyFix
    3. 3Calls such as res.send(404, 'Not found') or res.json(obj, 201)Status and body are passed to the same call in the old argument orderFix
    4. 4The stack from --trace-deprecation points into node_modulesA dependency makes the call, not your own codeFix

What does the error look like?

  • express deprecated res.send(status): Use res.sendStatus(status) instead
  • The warning prints once per call site, so only the first request through each line shows it
  • After upgrading to Express 5, res.send(200) and res.json(obj, 201) no longer behave as they did

Cause 1 of 4

A status number is passed to res.send()

1Check

Search your own code for res.send() called with a three-digit number and nothing else.

grep -rnE "res\.send\([0-9]{3}\)" --include="*.js" --exclude-dir=node_modules .

2Fix

Use res.sendStatus(), which sets the status and sends its text (Not Found for 404, and so on). If you want your own body next to the status, chain res.status() instead.

res.sendStatus(404);

// or, with your own body:
res.status(404).send('No such invoice');

3Verify

The response carries the status you chose, and the console no longer prints the warning for that line.

curl -i http://localhost:3000/missing

Confirmed by expressjs.com

Did this fix it?

Cause 2 of 4

A number was meant to be the response body

1Check

Express 4 reads a number given to res.send() as a status code; Express 5 does not support that signature at all. Look at what the argument stands for: a count, an id or a status.

grep -rn "res.send(" --include="*.js" --exclude-dir=node_modules .

2Fix

Quote the number to turn it into a string, as the Express 5 migration guide says, so Express does not take it for the old signature.

res.send(String(count));
// or a literal: res.send('42');

3Verify

The body of the response is the number as text.

curl -s http://localhost:3000/count

Confirmed by expressjs.com

Did this fix it?

Cause 3 of 4

Status and body are passed to the same call in the old argument order

1Check

Express 4.7 deprecated res.send(status, body), and the Express 5 guide lists res.json(obj, status) as no longer supported. Search for both shapes.

grep -rnE "res\.send\([0-9]{3}," --include="*.js" --exclude-dir=node_modules .
grep -rnE "res\.json\(.+, *[0-9]{3}\)" --include="*.js" --exclude-dir=node_modules .

2Fix

Set the status first with res.status(), then chain the body method.

res.status(404).send('Not found');
res.status(201).json({ name: 'Ruben' });

3Verify

Status and body both arrive as written.

curl -i -X POST http://localhost:3000/users -H 'Content-Type: application/json' -d '{"name":"Ruben"}'

Confirmed by github.com

Did this fix it?

Cause 4 of 4

A dependency makes the call, not your own code

1Check

The warning names the file and line that called the deprecated function. When that path is inside node_modules, the old call form is in a package, and editing your own routes will not remove the warning.

node --trace-deprecation server.js 2>&1 | grep node_modules
npm ls express

2Fix

Update the package that owns the call, or replace it. Do not edit files under node_modules: the change disappears on the next install.

npm update my-middleware-package

3Verify

Start the app again with the trace flag and send the request that printed the warning.

node --trace-deprecation server.js

Confirmed by github.com

Did this fix it?

How do I stop it coming back?

  • Always set the status with res.status(code) and finish with send(), json() or end(), never with the number inside send().
  • Start tests with node --trace-deprecation so new warnings show up before they become errors in Express 5.
  • Read the removed-methods list of the Express 5 migration guide before you upgrade, and grep for each item.
  • Keep middleware packages up to date, since the old call forms in them cause the same warning.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.