# RangeError: Maximum call stack size exceeded (Node.js)

Thrown when the V8 call stack fills up. The stack trace repeats the same one or two frames over and over; those repeating frames are the recursion you need to fix.

```
/app/src/models/user.js:9
  set email(value) {
           ^

RangeError: Maximum call stack size exceeded
    at User.set email (/app/src/models/user.js:9:12)
    at User.set email (/app/src/models/user.js:10:16)
    at User.set email (/app/src/models/user.js:10:16)
    at User.set email (/app/src/models/user.js:10:16)
    at User.set email (/app/src/models/user.js:10:16)
```

## Quick fix

Read the repeating frames in the trace: they name the function that keeps calling itself. Add or correct the condition that stops the recursion, and if the data is legitimately deep (large trees, long linked lists), switch to an iterative loop with an explicit stack array instead of raising Node's stack size.

1. Find the frames that repeat in the stack trace.
2. Check that every recursive path reaches a stop condition.
3. Look for setters, getters or toJSON methods that call themselves through this.
4. Replace spreading huge arrays into function arguments with a plain loop.
5. For deep data, rewrite the recursion as a while loop over an array.

```ts
// Recursive version overflows on very deep trees:
// function walk(node) { visit(node); node.children.forEach(walk); }

function walk(root: TreeNode) {
  const stack: TreeNode[] = [root];
  while (stack.length > 0) {
    const node = stack.pop()!;
    visit(node);
    for (const child of [...node.children].reverse()) stack.push(child);
  }
}
```

## Why it happens

### Missing or unreachable base case

The stop condition never becomes true: an off-by-one, a comparison against the wrong field, or input that skips the case the author expected (an empty string instead of null, a negative number).

### Accessor that calls itself

set email(value) { this.email = value } assigns through the setter again, forever. A getter that reads its own property, or a Proxy get trap that reads from the proxy, loops the same way. Store the value in a separate field such as #email.

### Spreading a huge array into arguments

Math.max(...values) and target.push(...items) pass every element as a separate argument. With very large arrays the arguments alone overflow the stack even though nothing is recursive; the exact size that breaks depends on the engine.

### Walking circular data without tracking visits

Hand-written deepClone, deepEqual or serializers follow parent pointers or ORM relations in a loop with no visited set. JSON.stringify detects cycles itself, but custom recursive code does not.

### Event or update ping-pong

A listener emits the event it listens to, or two pieces of state update each other synchronously (a change handler writes back to its source, which fires the handler again).

## Other fixes

- Use structuredClone for deep copies; it handles circular references.
- Compute max or min over large arrays with reduce or a for loop instead of spread.
- Track visited objects in a WeakSet when traversing graphs that may contain cycles.
- Raising the limit with node --stack-size is for diagnosis only; values set too high can segfault the process.

## Same error, other wording

- `Uncaught RangeError: Maximum call stack size exceeded`
- `RangeError: Maximum call stack size exceeded.`
- `InternalError: too much recursion`

## Sources

- [MDN: InternalError: too much recursion](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Too_much_recursion)
- [MDN: structuredClone()](https://developer.mozilla.org/en-US/docs/Web/API/Window/structuredClone)

---

To see this error the next time it happens in production, with the stack trace and the steps before it: Runheed records it and serves it to your coding agent over MCP. Setup instructions an agent can follow: https://runheed.com/SETUP.txt

Human-readable version: https://runheed.com/errors/maximum-call-stack-size-exceeded
