payload icon indicating copy to clipboard operation
payload copied to clipboard

bin.js breaks the CLI on Node ≥ 23.5: registerHooks = undefined workaround vs [email protected], plus ERR_REQUIRE_ASYNC_MODULE

Open kimku003 opened this issue 1 month ago • 4 comments

Environment

  • payload: 3.90.2 (also @payloadcms/[email protected])
  • tsx: 4.22.4 (pinned by payload, hoisted to root node_modules)
  • Node: v24.21.0
  • Related: #16949

Repro

All three CLI entry points fail, each differently:

./node_modules/.bin/payload generate:importmap
# → ERR_REQUIRE_ASYNC_MODULE (CJS / top-level await interop)

node --import tsx/esm node_modules/payload/bin.js generate:importmap
# → TypeError: T.registerHooks is not a function

bun ./node_modules/payload/bin.js generate:importmap
# → Cannot resolve 'tsx://…' (virtual module)

Root cause (two stacked bugs, not one)

a) node_modules/payload/bin.js:26-35 deliberately sets nodeModule.default.registerHooks = undefined to dodge the tsx 4.22.4 sync-hooks bug from #16949 (?namespace= leaking onto specifiers → ENOENT on Node ≥ 23.5). But [email protected] then calls registerHooks unconditionally, so the workaround itself throws TypeError: T.registerHooks is not a function.

b) ERR_REQUIRE_ASYNC_MODULE is a separate CJS/top-level-await interop issue, unrelated to tsx. It surfaces once (a) is fixed.

Updating tsx does not help

tsx is not a project dependency (pinned 4.22.4 by payload), and a root npm i -D tsx@latest doesn't change what the CLI resolves. Verified in a sandbox with [email protected]: the registerHooks error disappears but is replaced by Cannot destructure property 'loadEnvConfig' of 'import_env.default' as it is undefined (the ERR_REQUIRE_ASYNC_MODULE interop from (b)). The CLI stays broken — only the error message changes. Forcing the version via overrides would dodge a deliberate pin for zero benefit, so that's not a viable workaround either.

Current workaround (downstream)

Calling the generateImportMap API directly (node_modules/payload/dist/bin/generateImportMap/index.js) instead of going through bin.js. Works, but depends on an internal path.

Suggested directions

  • Gate the registerHooks = undefined workaround on a tsx version check (only apply it where the #16949 code path exists), or
  • Load tsx/esm/api through the async module.register() worker path explicitly instead of mutating the global node:module default, or
  • Address the ERR_REQUIRE_ASYNC_MODULE interop so at least the non-tsx path (./node_modules/.bin/payload) works on Node ≥ 23.5.

kimku003 avatar Oct 14 '26 11:10 kimku003

Thanks for the detailed write-up!

registerHooks error: this is already fixed on the 3.x branch in #18398, which removes the registerHooks workaround and bumps tsx to 4.23.15. It landed after 3.90.2 went out, so it'll ship in the next 3.x release.

One note on the node --import tsx/esm repro: preloading tsx yourself is what causes that error. tsx checks for registerHooks once, when it first loads, so if it loads before bin.js runs, the workaround removes the function tsx already decided to use. Running the CLI through payload / npx payload avoids that.

ERR_REQUIRE_ASYNC_MODULE: I can't reproduce this one. A minimal 3.90.2 project on Node 24 runs generate:importmap fine in both ESM and CJS setups. This error usually means something loaded with require() uses top-level await, so it's probably in your config or one of its imports. Could you share:

  • the full stack trace
  • your package.json (especially "type")
  • the imports in your payload.config.ts (plugins, adapters, anything with top-level await)

A minimal repro repo would be ideal if you can put one together.

Thank youuuu!

nathanlentz avatar Oct 14 '26 19:10 nathanlentz

Hi, I am working on a fix for this: gating the registerHooks workaround on the tsx version so the CLI stops breaking on newer Node versions. PR to follow. Rene

CloudCorpRecords avatar Oct 15 '26 23:10 CloudCorpRecords

Update from my side: I dug into this before writing a fix and I cannot reproduce it on the current release, so I am withdrawing my claim rather than sending a speculative PR.

What I checked: the registerHooks workaround only exists in the published [email protected] npm tarball. It is not present in the 3.x branch source, and it is not in the published 3.91.0 tarball either. Running [email protected] on Node v24.20.0, all three CLI entry points load and run without the reported ERR_REQUIRE_ASYNC_MODULE or registerHooks errors.

So this looks already resolved in 3.91.0, or it only reproduces under a more specific setup than the report describes. If anyone can still reproduce it on 3.91.0, a minimal repro would help pin it down. Happy to pick it back up if it resurfaces. Thanks, Rene

CloudCorpRecords avatar Oct 15 '26 23:10 CloudCorpRecords

Fixed in 3.91.0 by https://github.com/payloadcms/payload/pull/18398

AlessioGr avatar Oct 16 '26 00:10 AlessioGr