ERR_OUT_OF_RANGE
A numeric value is outside the allowed boundaries.
Usually happens because:
- ☑ Negative Sizes
- ☑ Crypto Lengths
- ☑ Out of Bounds Ports
🔍 Quick Checklist:
What is ERR_OUT_OF_RANGE?
The ERR_OUT_OF_RANGE error is thrown by Node.js when an input value (typically a number, index offset, or port) is outside the bounds of allowed arguments for a core API function.
Common Causes
- Negative Sizes: Passing negative values to buffer allocation functions.
- Crypto Lengths: Providing randomBytes lengths that exceed system allocation limits.
- Out of Bounds Ports: Attempting to bind sockets to ports outside the 0 - 65535 range.
| Cause | Frequency |
|---|---|
| Negative Sizes | ⭐⭐⭐⭐⭐ |
| Crypto Lengths | ⭐⭐⭐⭐ |
| Out of Bounds Ports | ⭐⭐⭐ |
Common Mistakes
- Passing user input directly to buffer allocations or crypto functions without validating the numbers bounds.
How to Fix
Framework-Specific Examples
Checking port variable ranges before binding the listener server.
const port = parseInt(process.env.PORT || '3000');
if (port < 0 || port > 65535) {
throw new Error('Port variable out of range');
}
app.listen(port);Server Configuration Examples
Checking port inputs.
# Handled at application layerBest Practices
- Implement schema constraints (e.g. Zod `.min()`, `.max()`) on API payload properties.
Frequently Asked Questions (FAQ)
Q: What triggers ERR_OUT_OF_RANGE?
It is thrown when a numeric parameter passed to a core Node.js function is outside the expected logical range (such as size constraints or negative array indexes).
Q: How does it differ from ERR_BUFFER_OUT_OF_BOUNDS?
ERR_OUT_OF_RANGE is a general error for function parameters. ERR_BUFFER_OUT_OF_BOUNDS is thrown specifically during buffer slice/read/write index overflows.
Q: Is this a RangeError?
Yes. It inherits from JavaScript's built-in `RangeError` class.
Q: How do I fix a port value out of range?
Ensure the port number parameter is an integer between 0 and 65535.