Skip to content

feat: Convert code to CJS - #51

Closed
timfish wants to merge 2 commits into
mainfrom
feat/cjs-first
Closed

feat: Convert code to CJS #51
timfish wants to merge 2 commits into
mainfrom
feat/cjs-first

Conversation

@timfish

@timfish timfish commented Jul 22, 2026

Copy link
Copy Markdown
Member

hook-sync.mjs is the only synchronous entry point to this package, but it's ESM-only. Consumers that ship CJS builds have no choice but to require('@apm-js-collab/tracing-hooks/hook-sync.mjs') - i.e. require(esm).

The Module.register() loader thread on Node 22.15–24.12 spawns a loader thread that re-runs --require preloads. If the preloads require graph reaches the require(esm) of hook-sync.mjs on that thread, Node throws:

The resolveSync() method is not implemented

because the loader thread's async-customized loader has no synchronous resolve chain. The thread finishes half-initialized, and any resolve request that reaches it afterwards crashes the host process:

TypeError: Cannot read properties of undefined (reading 'resolve')
    at MessagePort.handleMessage (node:internal/modules/esm/worker:179)

A (much more) viable alternative is that we instruct users not to use --require, specifically because it results in all that code being run on the loader thread too! This does not impact --import.

timfish added 2 commits July 22, 2026 01:16
hook.mjs has no genuine ESM dependencies of its own; converting it to
plain CJS lets consumers reach it via require() without triggering
Node's require(esm) bridge.
Adds hook-sync.cjs alongside hook-sync.mjs and a conditional
"./hook-sync" export so require('@apm-js-collab/tracing-hooks/hook-sync')
resolves synchronously to real CJS, without pulling consumers through
Node's require(esm) bridge. hook.mjs and hook-sync.mjs now delegate to
hook-core.js; their public APIs are unchanged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant