How to fix RangeError: The value of 'statusCode' is out of range. It must be >= 100 and <= 599. in Node.js

Node.jsBEGINNERMEDIUMUpdated Dec 9, 2025ยท 195 views

This RangeError occurs when you try to set an HTTP response status code that falls outside the valid range of 100-599. Node.js strictly validates status codes to ensure compliance with HTTP specifications.

Quick fix

Fixes ERR_HTTP_INVALID_STATUS_CODE

app.get('/api/user', (req, res) => {
  const statusCode = undefined; // Problem: undefined
  res.status(statusCode).json({ user: data }); // Error thrown here
});
  1. Identify where the invalid status code is being set
  2. Ensure status code variables are properly initialized
  3. Validate status codes are within the valid range
  4. Fix common Express.js pitfalls
node.js โ€” error
app.get('/api/user', (req, res) => {
const statusCode = undefined; // Problem: undefined
res.status(statusCode).json({ user: data }); // Error thrown here
});
RangeError: The value of 'statusCode' is out of range. It must be >= 100 and <= 599.

What this error means

This error is thrown by Node.js when you attempt to set an HTTP response status code that doesn't fall within the valid range of 100 to 599 (inclusive). The HTTP specification defines status codes in this range, organized into five classes: 1xx (Informational), 2xx (Success), 3xx (Redirection), 4xx (Client Error), and 5xx (Server Error). Node.js validates the status code whenever you set it using methods like res.statusCode, res.writeHead(), or res.status() in Express. If the value is less than 100, greater than 599, not an integer, or undefined, Node.js throws this RangeError to prevent invalid HTTP responses from being sent to clients. This validation ensures that your application sends well-formed HTTP responses that comply with standards and can be properly interpreted by browsers, proxies, and other HTTP clients.

Typical symptoms

  • Application crashes with RangeError when handling HTTP responses
  • Server fails to send response and connection hangs
  • Error occurs in Express middleware or route handlers
  • Stack trace points to res.status(), res.statusCode, or res.writeHead()
  • Error mentions ERR_HTTP_INVALID_STATUS_CODE

Common causes

  1. Passing undefined or null to res.status() or res.statusCode
  2. Setting statusCode to 0 or negative numbers
  3. Using status codes greater than 599
  4. Accidentally passing non-integer values as status codes
  5. Variable containing status code is not properly initialized
  6. Passing numeric response data directly to res.send() which Express interprets as status code
  7. Using outdated Express syntax like res.send(status, body) with invalid numbers

How to fix "RangeError: The value of 'statusCode' is out of range. It must be >= 100 and <= 599."

1Identify where the invalid status code is being set

Check your stack trace to locate the exact line where the error occurs. Look for calls to:
- res.status()
- res.statusCode = ...
- res.writeHead()
- res.sendStatus()

Example error location:

javascript
app.get('/api/user', (req, res) => {
  const statusCode = undefined; // Problem: undefined
  res.status(statusCode).json({ user: data }); // Error thrown here
});
2Ensure status code variables are properly initialized

If you're using a variable for the status code, make sure it's always set to a valid value. Use default values or validation:

javascript
// Bad: status might be undefined
const status = someCondition ? 200 : undefined;
res.status(status).json(data);

// Good: Always provide a valid default
const status = someCondition ? 200 : 500;
res.status(status).json(data);

// Good: Use logical OR for fallback
res.status(status || 200).json(data);
3Validate status codes are within the valid range

Add validation to ensure status codes are integers between 100 and 599:

javascript
function isValidStatusCode(code) {
  return Number.isInteger(code) && code >= 100 && code <= 599;
}

app.get('/api/data', (req, res) => {
  let statusCode = calculateStatus(); // Might return invalid value

  if (!isValidStatusCode(statusCode)) {
    console.error(`Invalid status code: ${statusCode}`);
    statusCode = 500; // Fallback to safe default
  }

  res.status(statusCode).json({ data });
});
4Fix common Express.js pitfalls

Avoid patterns that cause Express to misinterpret numeric values as status codes:

javascript
// Bad: Express treats 0 as status code
res.send(0);

// Good: Wrap numeric values in objects or arrays
res.json({ count: 0 });
res.json(0); // Also works with json()

// Bad: Old Express syntax (deprecated)
res.send(200, 'OK');

// Good: Chain status and send
res.status(200).send('OK');

// Bad: Sending multiple arguments
res.send({ id: 1 }, { id: 2 }); // First treated as status

// Good: Send single response
res.json([{ id: 1 }, { id: 2 }]);
5Handle edge cases in error handlers

Error handling middleware often sets dynamic status codes. Ensure they're always valid:

javascript
// Robust error handler
app.use((err, req, res, next) => {
  // Extract status code safely
  let statusCode = err.statusCode || err.status || 500;

  // Validate it's in range
  if (!Number.isInteger(statusCode) || statusCode < 100 || statusCode > 599) {
    console.error(`Invalid error status code: ${statusCode}`);
    statusCode = 500;
  }

  res.status(statusCode).json({
    error: err.message,
    status: statusCode,
  });
});

Advanced notes

Custom Status Codes: While Node.js restricts status codes to 100-599, the HTTP/1.1 specification (RFC 7231) primarily defines codes in the ranges 100-199, 200-299, 300-399, 400-499, and 500-599. Using undefined status codes (like 299 or 399) is technically valid but may not be understood by clients or proxies.

Fetch API Response Objects: When using the Fetch API or libraries like node-fetch, Response objects have stricter validation. The Fetch specification requires status codes in the range 200-599 for most responses, with even stricter rules for certain response types. This can cause compatibility issues when mocking responses or testing.

TypeScript Type Safety: Use TypeScript with strict typing to catch invalid status codes at compile time:

typescript
type HttpStatusCode = 100 | 101 | 200 | 201 | 204 | 301 | 302 | 304 | 400 | 401 | 403 | 404 | 500 | 503;

function setStatus(res: Response, code: HttpStatusCode) {
  res.status(code);
}

setStatus(res, 200); // OK
setStatus(res, 999); // Compile error

Debugging Tips: If the source of the invalid status code isn't obvious, add logging before setting status codes:

javascript
const originalStatus = res.status.bind(res);
res.status = function(code) {
  console.log('Setting status:', code, 'from:', new Error().stack);
  return originalStatus(code);
};

Third-Party Middleware: Some third-party middleware or libraries might set status codes incorrectly. Review your middleware chain and update or patch problematic packages. Common culprits include outdated error handling libraries and response transformation middleware.

Frequently asked questions

What does "ERR_HTTP_INVALID_STATUS_CODE" mean?
js when you attempt to set an HTTP response status code that doesn't fall within the valid range of 100 to 599 (inclusive). The HTTP specification defines status codes in this range, organized into five classes: 1xx (Informational), 2xx (Success), 3xx (Redirection), 4xx (Client Error), and 5xx (Server Error). status() in Express.
How do I fix "ERR_HTTP_INVALID_STATUS_CODE"?
This RangeError occurs when you try to set an HTTP response status code that falls outside the valid range of 100-599. Node.js strictly validates status codes to ensure compliance with HTTP specifications.
What causes "ERR_HTTP_INVALID_STATUS_CODE"?
Passing undefined or null to res.status() or res.statusCode Setting statusCode to 0 or negative numbers Using status codes greater than 599
Is the "ERR_HTTP_INVALID_STATUS_CODE" error serious?
It is a medium-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading