Error explained
RangeError: Maximum call stack size exceeded
The JavaScript engine ran out of stack space, almost always because a function keeps calling itself, directly or through other functions.
3 min read9 sectionsUpdated
On this page
| Cause | Example | Fix |
|---|---|---|
| Recursion without a base case | walk(node) never stops on cycles | Add a base case; track visited nodes |
| Setter that assigns itself | set name(v) { this.name = v } | Use a backing field (this._name) |
| Mutual recursion through events | Handler A triggers B triggers A | Guard with a flag or compare old and new values |
| Circular references in serialization | Custom toJSON recursing over a graph | Detect cycles; serialize IDs |
| Huge argument spread | Math.max(...bigArray) | Use a loop or reduce |
| Framework re-render loops | setState in render or an effect without deps | Fix the dependency or condition |
// recursive: stack depth grows with tree depth
function countNodes(node) { return 1 + node.children.reduce((s, c) => s + countNodes(c), 0); }
// iterative: an explicit stack on the heap
function countNodesIter(root) {
let count = 0;
const stack = [root];
while (stack.length) {
const n = stack.pop();
count += 1;
stack.push(...n.children);
}
return count;
}Catching it in production
In the browser this error often freezes or blanks a page; real user monitoring records it with the stack, and source maps show the repeating frames in your own code.
VitalRix error tracking groups exceptions recorded on OpenTelemetry spans into issues with counts, first and last seen, affected endpoints and versions, and marks an issue as regressed if it comes back after being resolved. Alert rules on error rate per service or endpoint route the problem to the right team before customers report it.
In React and other frameworks
A component that sets state during render, or an effect that updates a value it depends on without a guard, re-renders forever. React usually reports "Too many re-renders" or "Maximum update depth exceeded", and the browser can also hit this RangeError. Look for setState called directly in the component body, effects with missing or changing dependencies, and two-way bindings that update each other.
JSON and circular structures
JSON.stringify throws a different error ("Converting circular structure to JSON") on cycles, but custom serializers and deep clone helpers often recurse until the stack overflows instead. Use structuredClone for deep copies, and replacer functions that track seen objects when serializing graphs.
Can I increase the stack size?
In Node you can with --stack-size, but it only delays the problem and can crash the process. Fix the recursion.
Why does it happen only with some data?
Recursion depth follows the data: a deeply nested or cyclic structure triggers it. Iterate, or guard against cycles.
Related reading
- java.lang.StackOverflowError: causes and how to fix it: Fix java.lang.StackOverflowError: read the repeating frames, find runaway recursion, circular toString and JSON mappings, and know when -Xss is appropriate.
- RecursionError: maximum recursion depth exceeded in Python: Fix Python RecursionError: maximum recursion depth exceeded. Find accidental recursion in properties and getattr, use iteration and set the limit safely.
- PHP maximum execution time exceeded: how to fix it: Fix PHP Maximum execution time of 30 seconds exceeded: why external waits may not count, which timeouts line up with Nginx, and how to move long work away.
- 431 Request Header Fields Too Large: how to fix it: Fix 431 Request Header Fields Too Large: bloated cookies, Node.js header limits and localhost cookie build-up, with commands to measure real header size.
- JavaScript heap out of memory: how to fix it in Node.js: Fix FATAL ERROR: JavaScript heap out of memory. Set --max-old-space-size correctly, handle build-time failures and find runtime leaks with heap snapshots.
- 413 Request Entity Too Large: how to raise the limit safely: Fix 413 Request Entity Too Large in Nginx, Apache, Node.js, PHP and Kubernetes ingress: find which layer sets the limit and raise it safely for uploads.
Watch your own site and services with VitalRix
Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.