Express crashes with RangeError: Invalid status code: undefined
RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: undefinedShort 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.
Fix it fast
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);2Match what you see, then jump to its fix
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/boomConfirmed by expressjs.com
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-failedConfirmed by nodejs.org
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 fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
Axios call from a React app comes back 404
404 (Not Found)
Status code 404 means a server answered, but it has nothing at that path.
3 causes, each with a fixBack end
bcrypt.hash throws 'data and salt arguments required'
Error: data and hash arguments required
The bcrypt package throws this when hash gets null or undefined for the password or for the salt.
4 causes, each with a fixBack end
bcryptjs rejects hash() or compare() with Illegal arguments
Error: Illegal arguments: string, undefined
bcryptjs checks the types of its two arguments and, when either is wrong, throws Illegal arguments followed by the typeof of the first and the second.
3 causes, each with a fixBack end
Express warns 'body-parser deprecated undefined extended'
body-parser deprecated undefined extended
The warning appears because urlencoded() was called without the extended option, which body-parser 1.x has warned about since version 1.4.0.
3 causes, each with a fix