Skip to content

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

Key points

  • Infinite or very deep recursion is the cause in most cases.
  • The stack trace repeats the same frames; find the cycle.
  • Spreading huge arrays into a call can also overflow.
  • Convert deep recursion to iteration when depth depends on data.

Causes and fixes

CauseExampleFix
Recursion without a base casewalk(node) never stops on cyclesAdd a base case; track visited nodes
Setter that assigns itselfset name(v) { this.name = v }Use a backing field (this._name)
Mutual recursion through eventsHandler A triggers B triggers AGuard with a flag or compare old and new values
Circular references in serializationCustom toJSON recursing over a graphDetect cycles; serialize IDs
Huge argument spreadMath.max(...bigArray)Use a loop or reduce
Framework re-render loopssetState in render or an effect without depsFix the dependency or condition

Recursion to iteration

javascript
// 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.

Frequently asked questions

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.

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.