Maaz Zindani

Loading the index…

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

Express crashes with RangeError: Invalid status code: undefined

ErrorRangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: undefined

Short answer

Node throws this when a response is given a status that is not a number from 100 to 999. In Express it is almost always res.status(err.status) in an error handler, where err.status is undefined for ordinary errors. Fall back to 500, and map any non-HTTP error value yourself before using it as a status.

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

Fix it fast

  1. 1Find out which cause you have

    Read the stack trace to the first line in your own code, then print the value it passes to status or statusCode.

    console.log('status value:', err.status, typeof err.status);
  2. 2Match what you see, then jump to its fix

    1. 1Invalid status code: undefined, and it appears when some other error occursAn error handler uses err.status, which most errors do not haveFix
    2. 2Invalid status code: followed by a string or a number such as ECONNREFUSED, 'E11000' or 0A value that is not an HTTP status is used as the statusFix

What does the error look like?

  • RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: undefined

Cause 1 of 2

An error handler uses err.status, which most errors do not have

1Check

Express reads the status from err.status or err.statusCode, and a plain new Error sets neither, so res.status(err.status) receives undefined, and the original error is hidden by this new one.

grep -rn "err.status" app.js src/

2Fix

Give the status a default. Express's own default handler also uses err.status or err.statusCode and sets 500 when the value is outside the 4xx and 5xx range.

app.use((err, req, res, next) => {
  if (res.headersSent) return next(err);
  const status = err.status ?? err.statusCode ?? 500;
  res.status(status).json({ error: err.message });
});

3Verify

Throw a plain error in a route and check that you get 500 with the message.

app.get('/boom', () => { throw new Error('boom'); });
curl -i http://localhost:3000/boom

Confirmed by expressjs.com

Did this fix it?

Cause 2 of 2

A value that is not an HTTP status is used as the status

1Check

Node accepts a status code in the range 100 to 999 only. A value taken from some other field, such as an error code string or a number from a library, usually falls outside that range.

console.log(typeof code, code);

2Fix

Never pass such a field to res.status(). Decide the status yourself, and if you do accept a number from outside, check the range first.

const isHttp = (n) => Number.isInteger(n) && n >= 400 && n <= 599;
const status = isHttp(err.status) ? err.status : 500;
res.status(status).json({ error: 'Request failed' });

3Verify

Trigger the failing path again and check the status line.

curl -i http://localhost:3000/path-that-failed

Confirmed by nodejs.org

Did this fix it?

How do I stop it coming back?

  • Write one error handler for the whole app and give status a default there.
  • Set err.status on errors that should return 4xx, and let everything else become 500.
  • Check res.headersSent in the handler before writing a response.

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

Back end

Express crashes with ERR_HTTP_HEADERS_SENT

Error: Can't set headers after they are sent to the client

This error means your code tried to write headers or a second response after Express already sent one.

3 causes, each with a fix

Next step

Want this handled for good?

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