Turbopack runtime caches a stack overflow from a module's first server load as a permanent module error
Link to the code that reproduces this issue
https://github.com/J-Fannilla/next-ssr-stack-overflow-poisons-module
To Reproduce
pnpm install
pnpm repro
The app is a bare create-next-app (Turbopack, App Router, default config) with three client components:
Towernests itselfdepthtimes (/?depth=N), standing in for the providers and wrappers of a large app.Leafrenders at the bottom of the tower. Its module imports a chain of 60 small modules, like a component that pulls in a library.Probereports how much stack is left where it renders. The script uses it to pick the depth where the bottom of the tower has less free stack than the leaf's first load needs. That depth depends on the machine, the Node version and dev or production, so it is measured rather than hard coded.
pnpm repro builds the app and runs the same check against next dev and next start:
- In one server process, request
/?depth=<depth>. The first load of the leaf's module runs out of stack. - Request
/?depth=<depth>once more, then/?depth=0three times. - In a fresh process, request
/?depth=0first, then/?depth=<depth>twice.
Current vs. Expected behavior
Current, next dev on 16.5.0-canary.46 (next start prints the same pattern at depth 2,662):
Run 1: the first load of the leaf module happens at the edge of the stack.
depth 714: 484 probe frames free at the bottom of the tower
GET /?depth=714 -> 500, leaf rendered: false
GET /?depth=714 -> 500, leaf rendered: false
GET /?depth=0 -> 500, leaf rendered: false
GET /?depth=0 -> 500, leaf rendered: false
GET /?depth=0 -> 500, leaf rendered: false
5 render errors, identical stacks: true
Run 2: a fresh process loads the leaf module at depth 0 first.
GET /?depth=0 -> 200, leaf rendered: true
GET /?depth=714 -> 200, leaf rendered: true
GET /?depth=714 -> 200, leaf rendered: true
0 render errors
After one render runs out of stack inside a module's first load, every later render that uses that module fails with RangeError: Maximum call stack size exceeded, including at depth 0, until the server process restarts. The five errors have byte-identical stacks: it is the stored error thrown again. A fresh process that loads the module at a shallow depth first renders the same deep tree fine.
The error stack in next dev:
RangeError: Maximum call stack size exceeded
at instantiateModule (.next/dev/server/chunks/ssr/[turbopack]_runtime.js:1514:9)
at getOrInstantiateModuleFromParent (.next/dev/server/chunks/ssr/[turbopack]_runtime.js:1823:23)
at Context.commonJsRequire [as r] (.next/dev/server/chunks/ssr/[turbopack]_runtime.js:500:12)
at Object.require (next/dist/compiled/next-server/app-page-turbo.runtime.dev.js:318:9017)
at globalThis.__next_require__ (next/dist/compiled/next-server/app-page-turbo.runtime.dev.js:318:10629)
at requireModule (next/dist/compiled/next-server/app-page-turbo.runtime.dev.js:2:351097)
at initializeModuleChunk (next/dist/compiled/next-server/app-page-turbo.runtime.dev.js:2:393636)
at readChunk (next/dist/compiled/next-server/app-page-turbo.runtime.dev.js:2:384482)
at readChunkAndTransferValidation (next/dist/compiled/next-server/app-page-turbo.runtime.dev.js:2:396051)
at getComponentNameFromType (next/dist/compiled/next-server/app-page-turbo.runtime.dev.js:2:79606)
In next start the tip is instantiateModule and getOrInstantiateModuleFromParent in .next/server/chunks/ssr/[turbopack]_runtime.js.
Expected: a stack overflow is a property of where a module was first required, not of the module. The next require should evaluate the module again. React's server renderer already recovers from overflows in its own render code: it catches Maximum call stack size exceeded and continues the task on a fresh stack. That retry requires the module again and should succeed, so the render should return 200.
Where the error is stored
There are two places, and both have to let go of the error for the render to recover.
-
The Turbopack runtime, for the life of the process.
instantiateModuleregisters the module in the cache before it runs the factory, and stores the factory's error on it:moduleCache.set(id, module) try { moduleFactory(context, module, exports) } catch (error) { module.error = error throw error }Every later require finds the cached module and throws
module.erroragain. Oncanary(bd04a62bb7, 2026-11-11) this is inturbopack/crates/turbopack-ecmascript-runtime/js/src/nodejs/runtime/build-base.ts(instantiateModule),shared/runtime/hmr-runtime.ts(the devinstantiateModule) andshared/runtime/runtime-utils.ts(throw module.error). Every module that was mid evaluation when the stack ran out keeps the error, from the one that overflowed up to the client component that was being required. -
React's Flight client, for the rest of the request.
initializeModuleChunkinreact-server-dom-turbopack-client.noderejects the chunk with whateverrequireModulethrew:function initializeModuleChunk(chunk) { try { var value = requireModule(chunk.value); chunk.status = "fulfilled"; chunk.value = value; chunk.reason = null; } catch (error) { (chunk.status = "rejected"), (chunk.reason = error); } }React's renderer then retries the task on a fresh stack, reads the rejected chunk and reports the same error.
The second one has a draft in React: https://github.com/react/react/pull/38063 lets the renderer retry a lazy chunk that hit a stack overflow. With Turbopack that retry would still fail, because the runtime throws module.error again. So the Turbopack side needs a change of its own.
Why a stack overflow should not be cached
- It does not describe the module. The same module evaluates fine from a shallower call site, as the second run shows. Caching it turns one deep render into a failure of every page that uses the module, at any depth, until a restart.
- It defeats React's own recovery. The renderer continues on a fresh stack exactly so that deep trees render. The retry needs the module load to be repeatable.
- Node's CommonJS loader does not keep the error: it removes a module from
require.cachewhen its evaluation throws, so the nextrequireruns it again. - Retrying is cheap and only happens on failure.
A possible change in instantiateModule: when the factory throws a stack overflow (a RangeError with the message Maximum call stack size exceeded, which is how React detects it too), remove the module from moduleCache instead of setting module.error, then rethrow. Top level side effects of the module could then run twice, which is already true of a CommonJS module that throws in Node. Narrowing it to stack overflows keeps the current behavior for ordinary evaluation errors.
Provide environment information
Operating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 27.2.0: Wed Oct 14 23:47:58 PDT 2026; root:xnu-13432.41.4~25/RELEASE_ARM64_T6050
Available memory (MB): 36864
Available CPU cores: 18
Binaries:
Node: 26.13.0
npm: 11.20.0
Yarn: 4.18.1
pnpm: 12.18.0
Relevant Packages:
next: 16.5.0-canary.46
eslint-config-next: N/A
react: 19.3.0
react-dom: 19.3.0
typescript: 5.9.3
Next.js Config:
output: N/A
Which area(s) are affected? (Select all that apply)
Turbopack, Runtime
Which stage(s) are affected? (Select all that apply)
next dev (local), next start (local)
Additional context
Checked on macOS arm64, with Turbopack and the default config:
| Next | Node | next dev |
next start |
|---|---|---|---|
| 16.4.4 | 22.20.0 | reproduces | reproduces |
| 16.4.4 | 26.13.0 | reproduces | reproduces |
| 16.5.0-canary.46 | 26.13.0 | reproduces | reproduces |
Not tested: Linux, x64, webpack, the edge runtime. The depth changes with the platform; the behavior should not.
Possibly related: #98492 shows the same error in next dev with cacheComponents and a route eight segments deep, with the overflow in requireModule under getComponentNameFromType. Its "client component rendered by the page" row, which never heals, looks like this issue. This reproduction needs neither cacheComponents nor nested segments, and it fails in production too.
Could I investigate this? I’d first isolate the module-evaluation behavior in a Turbopack fixture and compare it with webpack to clarify when a later load should be allowed to retry and when a cached failure should remain.
I see that in your repro repo the range error is emitted during import evaluation. Per the ESM spec these errors must be cached.
Furthermore, A RangeError very much could be the fault of the module in question.
For require in nodejs the rules are different and it could be argued that we are thus diverging
For webpack the 'cached error' semantics are configurable via output.strictModuleErrorHandling based on whether your want ESM semantics or not.
in https://github.com/vercel/next.js/pull/99671 we did substantially improve the dev runtime to decrease the likelihood of stack overflow, so you might try a canary release to see if you still hit the error.