RangeError: Maximum call stack size exceeded (JavaScript) — how to fix
Uncaught RangeError: Maximum call stack size exceeded
at recurse (app.js:2:3)
at recurse (app.js:3:10)
at recurse (app.js:3:10)
Quick fix: Look at the repeating frames in the stack trace to find the loop. Add or fix the base case of the recursive function, make getters/setters use a different backing field (this._name), and replace Math.max(...hugeArray) / arr.push(...hugeArray) with a loop or reduce.
What it means
Each function call adds a frame to the call stack. The stack has a fixed size (in V8 roughly ten thousand frames, depending on frame size). When a chain of calls never returns, the stack fills up and the engine throws RangeError: Maximum call stack size exceeded. Firefox reports the same problem as InternalError: too much recursion.
The stack trace shows the same few lines repeating. Those lines are the loop.
Cause 1: recursion without a working base case
function countdown(n) {
console.log(n);
countdown(n - 1); // never stops
}
Fix: add a condition that stops the recursion and make sure every call moves toward it:
function countdown(n) {
if (n < 0) return;
console.log(n);
countdown(n - 1);
}
A common variant is a base case that can never match, e.g. if (n === 0) when n can be a fraction or negative. Use n <= 0.
Cause 2: a getter or setter that calls itself
class User {
set name(value) { this.name = value; } // calls the setter again
get name() { return this.name; } // calls the getter again
}
Store the value in a different property:
class User {
#name;
set name(value) { this.#name = value; }
get name() { return this.#name; }
}
The same happens with a toString() or toJSON() that uses template literals or JSON.stringify on this.
Cause 3: two functions calling each other
a() calls b(), which calls a() again. In UI code this is often an event handler that triggers the event it listens to, e.g. a change listener that sets the value and dispatches change again. Guard with a flag, or only update when the value actually differs.
Cause 4: spreading a very large array into a call
Arguments are placed on the stack, so passing hundreds of thousands of them can overflow it even without recursion:
const max = Math.max(...bigArray); // can throw for very large arrays
target.push(...bigArray); // same
Use a loop or reduce instead:
const max = bigArray.reduce((m, x) => (x > m ? x : m), -Infinity);
for (const x of bigArray) target.push(x);
Cause 5: recursive data structures
Walking an object graph that contains cycles (a node pointing back to its parent) recurses forever. Track visited objects:
function walk(node, seen = new Set()) {
if (!node || seen.has(node)) return;
seen.add(node);
node.children?.forEach((c) => walk(c, seen));
}
Cause 6: recursion that is correct but too deep
If the algorithm really needs a depth of tens of thousands, rewrite it iteratively with an explicit stack array. In Node.js you can raise the limit with node --stack-size=..., but this can crash the process with a segfault if set larger than the OS thread stack, so prefer the iterative rewrite.
Last updated 2026-12-02.