Skip to content

RangeError: Maximum call stack size exceeded in ESM module loader on large single-file bundled ESM output #62457

@crisxuan

Description

@crisxuan

Version

Node version: v22.22.1

Platform

Linux x86_64 (Linux 5.15.0-100-generic)

Subsystem

ESM module loader / node:internal/modules/esm/utils compileSourceTextModule

What steps will reproduce the bug?

  1. Install openclaw from npm: npm install -g openclaw
  2. Try to run the gateway: openclaw gateway start
  3. The process immediately crashes with RangeError: Maximum call stack size exceeded

Package: https://www.npmjs.com/package/openclaw

How often does it reproduce? Is there a required condition?

100% reproducible on every startup with Node.js 22.22.1.

Required conditions:

  • Node.js 22.x (tested on 22.22.1)
  • Loading a large single-file bundled ESM package that contains many cyclic re-exports (common output from modern bundlers
    like esbuild/rollup)

What is the expected behavior? Why is that the expected behavior?

Expected: Node.js should successfully load the module and start the application.

This is the expected behavior because:

  • The same code/package works correctly on Node.js 20.x
  • Single-file ESM bundling is a common distribution method for CLI tools on npm
  • Cyclic dependencies/re-exports should be handled gracefully by the module loader without infinite recursion

What do you see instead?

The ESM module loader goes into infinite recursion during loading and eventually crashes with:

RangeError: Maximum call stack size exceeded
at compileSourceTextModule (node:internal/modules/esm/utils:346:16)
at ModuleLoader.moduleStrategy (node:internal/modules/esm/translators:107:18)
at #translate (node:internal/modules/esm/loader:546:20)
at afterLoad (node:internal/modules/esm/loader:596:29)
at ModuleLoader.loadAndTranslate (node:internal/modules/esm/loader:601:12)
at #createModuleJob (node:internal/modules/esm/loader:624:36)
at #getJobFromResolveResult (node:internal/modules/esm/loader:343:41)
at ModuleLoader.getModuleJobForImport (node:internal/modules/esm/loader:311:41)
... (repeats thousands of times until stack overflow)

Even with --stack-size=131072 (4x default), it still overflows.

Additional information

  • This works fine in Node.js 20.x, broken in Node.js 22.x
    • The bundle is a single large ESM file (~30MB) containing the entire application with all dependencies inlined
    • This packaging method is very common for CLI tools that ship everything in one file
    • Suspected change in ESM loading algorithm in Node.js 22 introduced the infinite recursion on this dependency structure

Environment:

  • Node.js: v22.22.1
  • OS: Linux 5.15.0-100-generic x86_64 (Ubuntu 22.04)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions