Skip to content

fix: load static-wado-util via createRequire so Bun can require() it - #133

Open
wayfarer3130 wants to merge 1 commit into
masterfrom
fix/bun-require-async-module
Open

fix: load static-wado-util via createRequire so Bun can require() it#133
wayfarer3130 wants to merge 1 commit into
masterfrom
fix/bun-require-async-module

Conversation

@wayfarer3130

Copy link
Copy Markdown
Collaborator

Problem

mkdicomweb fails at startup under Bun 1.3:

TypeError: require() async module ".../static-wado-util/lib/index.ts" is unsupported. use "await import()" instead.
      at packages/static-wado-creator/lib/program/index.js:1:7

@radicalimaging/static-wado-util resolves to lib/index.ts and is "type": "module", while 48 files in static-wado-creator reach it through a CommonJS require(). lib/index.mjs also imported it as ESM, for a single uids binding. That one import pulls the util into this module's async ESM graph, and Bun then rejects every CJS require() of it.

Nothing in static-wado-util uses top-level await — the async classification comes purely from the mixed ESM/CJS load order. Requiring the package from a plain .cjs entry resolves fine (47 exports), which is what makes the failure specific to the graph rooted at mkdicomweb.mjs.

Fix

Load it through createRequire so it stays out of the ESM graph and the 48 CJS requires resolve synchronously again.

Two alternatives were tried and rejected:

  • bun --preload of the util first — no effect; Bun classifies it async regardless of load order.
  • Converting the require in program/index.js to await import() — just moves the error to mkdicomwebConfig.js, and there are 47 more behind it.

Scope

static-wado-creator is the only package in the monorepo with this mix — a scan of all packages for ESM importers and CJS requirers of static-wado-util returns only this one.

Test

bun packages/static-wado-creator/bin/mkdicomweb.mjs create --help

Fails on master, prints usage and exits 0 with this change.

🤖 Generated with Claude Code

static-wado-util resolves to lib/index.ts and is ESM, while 48 files in
static-wado-creator reach it through a CommonJS require(). lib/index.mjs
also imported it as ESM, which pulled it into that module's async graph;
Bun 1.3 then rejects every one of those requires with

  TypeError: require() async module ".../static-wado-util/lib/index.ts"
  is unsupported. use "await import()" instead.

which makes mkdicomweb fail at startup. Nothing in static-wado-util uses
top-level await -- the async classification comes purely from the mixed
ESM/CJS load order, and requiring the package from a plain CJS entry has
always worked.

Loading it through createRequire keeps it out of the ESM graph, so all 48
CJS requires resolve synchronously again. static-wado-creator is the only
package in the monorepo with this mix.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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