fix: add CJS bundle entry for Node 24 / CommonJS Gateway hosts#171
Open
seraphjiang wants to merge 1 commit into
Open
fix: add CJS bundle entry for Node 24 / CommonJS Gateway hosts#171seraphjiang wants to merge 1 commit into
seraphjiang wants to merge 1 commit into
Conversation
On Node.js 24 the existing ESM dist/index.js triggers a jiti fallback that crashes with 'Cannot read properties of undefined (reading createRequire)'. Add a pre-built CJS bundle (dist-cjs/index.cjs) so OpenClaw can require() the plugin without invoking jiti at all. - esbuild bundles index.ts to CJS; openclaw, zod, qrcode-terminal stay external so the host's copies are used at runtime - import.meta.url is shimmed via pathToFileURL(__filename) - dist-cjs/index.cjs is listed first in openclaw.runtimeExtensions; ESM hosts fall through to the existing dist/index.js unchanged - build:cjs script added; build now runs both tsc and esbuild - dist-cjs/ added to package files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
On Node.js 24 with a CommonJS Gateway host, the plugin silently fails to load.
Root cause chain:
require()ondist/index.jsfirst (synchronous native load).dist/index.jsis ESM ("type": "module"), sorequire()throwsERR_REQUIRE_ESM.Fix
Add a pre-built CJS bundle (
dist-cjs/index.cjs) generated with esbuild:openclaw,@openclaw,zod, andqrcode-terminalare marked--externalso the bundle stays small (~174 KB) and uses the host's copies at runtime.import.meta.urlis shimmed withrequire('url').pathToFileURL(__filename).hrefso the one internal usage (src/api/api.ts) resolves correctly.dist-cjs/index.cjsis added as the first entry inopenclaw.runtimeExtensions. OpenClaw picks the first file that loads successfully; on CommonJS hostsrequire('./dist-cjs/index.cjs')succeeds natively — no jiti involved. On ESM hosts the existingdist/index.jsentry is used as before.Changes
package.jsondist-cjs/tofiles; prepend./dist-cjs/index.cjstoruntimeExtensions; addbuild:cjsscript; addesbuilddevDependencydist-cjs/index.cjsTesting
Verify
require()loads cleanlyExpected output:
Rebuild and re-verify
npm run build node -e "const m = require('./dist-cjs/index.cjs'); console.log((m.default||m).id)"End-to-end (Node 24 Gateway)
openclaw-weixinchannel in config.openclaw gateway statuswithstatus: active(noERR_REQUIRE_ESMor jiti crash in logs).Notes
dist-cjs/output is committed so consumers don't need a build step afternpm install.