Skip to content

fix(rivetkit): emit CJS-safe import.meta.url so CommonJS consumers can load rivetkit#5379

Draft
khaled-mansour-zid wants to merge 1 commit into
rivet-dev:mainfrom
khaled-mansour-zid:fix/rivetkit-cjs-import-meta
Draft

fix(rivetkit): emit CJS-safe import.meta.url so CommonJS consumers can load rivetkit#5379
khaled-mansour-zid wants to merge 1 commit into
rivet-dev:mainfrom
khaled-mansour-zid:fix/rivetkit-cjs-import-meta

Conversation

@khaled-mansour-zid

Copy link
Copy Markdown

Summary

rivetkit's CJS build ships a verbatim import.meta.url, which is a load-time SyntaxError in any CommonJS consumer (e.g. importing rivetkit under ts-node / a CJS host process).

Root cause

getRequireFn() in rivetkit-typescript/packages/rivetkit/src/utils/node.ts uses createRequire(import.meta.url) (and carries a // TODO: This causes issues in tsup with the CJS require branch commented out). The package's tsup.config.ts deliberately sets shims: false — correctly, since the ESM shims pull in Node-only modules that break the browser build (rivetkit/client). But with shims off, tsup emits import.meta.url verbatim into the .cjs output, so:

dist/tsup/mod.cjs:  return _module.createRequire.call(void 0, import.meta.url);

fails to load in CommonJS.

Fix

Keep shims: false (don't regress the browser build) and instead define a CJS-safe replacement for import.meta.url only for the cjs format, via tsup's format-aware esbuildOptions(options, context):

if (context.format === "cjs") {
  options.define = {
    ...(options.define || {}),
    "import.meta.url": "require('url').pathToFileURL(__filename).href",
  };
}

ESM output is untouched (keeps native import.meta.url); only the CJS bundle gets the load-safe form. Single-file change to tsup.config.ts.

Notes

  • Repro: require('rivetkit') from a CommonJS module against the current published build throws on import.meta.
  • There is a companion packaging gap (some @rivetkit/* subpackages declare a require export condition but don't ship .cjs) — happy to follow up separately; this PR fixes the main rivetkit entry.
  • Draft pending your CI (I can't build the full monorepo locally); the change is minimal and format-scoped.

…n load rivetkit

getRequireFn() (src/utils/node.ts) uses createRequire(import.meta.url), but the
tsup build sets shims:false (to keep ESM shims out of the browser build), so the
CJS output keeps a verbatim import.meta.url — a SyntaxError at load for any
CommonJS consumer (e.g. importing rivetkit under ts-node). Define a CJS-safe
replacement for the cjs format only; ESM is untouched. Addresses the existing
'TODO: This causes issues in tsup' in getRequireFn.
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