The npm ESM build cannot load on workerd — tsup's createRequire banner runs at module scope #93

Closed
opened 2026-09-05 20:08:22 +00:00 by chrnx · 1 comment
Owner

@green-tea/core/edge installed from npm does not load on workerd. The
worker throws before any green-tea code runs:

Uncaught TypeError: The argument 'path' must be a file URL object, a file URL
string, or an absolute path string.. Received 'undefined'
  at node:module:34:15 in createRequire

Cause

tsup.config.ts gives the ESM build a createRequire banner:

import { createRequire as _createRequire } from 'node:module';
const require = _createRequire(import.meta.url);

That runs at module scope, in every ESM entry point including dist/edge.mjs,
and import.meta.url is undefined on workerd. From the wrangler bundle:

2287: import { createRequire as _createRequire } from "node:module";
2288: var require2 = _createRequire(import.meta.url);

src/node-require.ts is written to make exactly this safe — it resolves lazily,
prefers an ambient require, and its doc comment says workerd is the runtime
where it "throws on a runtime that can offer neither", with every caller guarding
and reporting in its own terms. The banner defeats that by calling createRequire
eagerly, at load, for a code path (edgeHandler) that never requires anything.

Reproduction

mkdir edge-spike && cd edge-spike
npm init -y && npm i @green-tea/core@26.9.0-beta.1 && npm i -D wrangler
# src/main.ts: createApp with one @Get, `export default { fetch: edgeHandler(app) }`
# wrangler.toml: compatibility_flags = ["nodejs_compat"]
npx wrangler deploy --dry-run --outdir=out   # bundles fine, 232 KiB
npx wrangler dev --local                     # worker throws at load, no server

The same project installed from JSR boots and serves:

npx jsr add @green-tea/core     # -> "npm:@jsr/green-tea__core@..."
npx wrangler dev --local
curl -i localhost:8787/         # 200, with the security headers

JSR publishes transpiled source with no bundler and therefore no banner, which
is why that path is unaffected — and which confirms the banner is the whole
cause rather than one contributor.

Why the edge test is green

test/edge/run.ts bundles test/edge/worker.ts, which imports ../../src/index
and ../../src/edge — the sources. dist/ is never loaded by any test, so
the one artifact users install is the one nothing exercises. The test's own
comment reasons carefully about nodejs_compat and node:http, all of it
correct, and none of it reaches the banner because the banner does not exist on
the path it tests.

Suggested fix

Make the banner match what node-require.ts already assumes — that a missing
require is a fact to discover, not a crash:

const require = import.meta.url ? _createRequire(import.meta.url) : undefined;

typeof require === 'function' then fails on workerd, resolveRequire falls
through to process.getBuiltinModule, that returns nothing, and nodeRequire
throws only when something actually calls it — which is the designed behaviour,
and which edgeHandler never does.

Worth pairing with a test that loads dist/edge.mjs rather than src/edge.ts.
Bundling the built artifact is the only way this class of defect is visible, and
it is a packaging problem in the same family as #69 (the JSR createRequire
crash) — that one was source-only breaking, this one is build-only breaking, and
the test suite can currently see neither from the other side.

Found while adding a --runtime edge target to the
matcha CLI. That scaffold installs
core from JSR for now and says why in its wrangler.toml; it can go back to a
plain npm dependency once this is fixed.

`@green-tea/core/edge` installed from **npm** does not load on workerd. The worker throws before any green-tea code runs: ``` Uncaught TypeError: The argument 'path' must be a file URL object, a file URL string, or an absolute path string.. Received 'undefined' at node:module:34:15 in createRequire ``` ## Cause `tsup.config.ts` gives the ESM build a `createRequire` banner: ```js import { createRequire as _createRequire } from 'node:module'; const require = _createRequire(import.meta.url); ``` That runs at **module scope**, in every ESM entry point including `dist/edge.mjs`, and `import.meta.url` is `undefined` on workerd. From the wrangler bundle: ``` 2287: import { createRequire as _createRequire } from "node:module"; 2288: var require2 = _createRequire(import.meta.url); ``` `src/node-require.ts` is written to make exactly this safe — it resolves lazily, prefers an ambient `require`, and its doc comment says workerd is the runtime where it "throws on a runtime that can offer neither", with every caller guarding and reporting in its own terms. The banner defeats that by calling `createRequire` eagerly, at load, for a code path (`edgeHandler`) that never requires anything. ## Reproduction ```bash mkdir edge-spike && cd edge-spike npm init -y && npm i @green-tea/core@26.9.0-beta.1 && npm i -D wrangler # src/main.ts: createApp with one @Get, `export default { fetch: edgeHandler(app) }` # wrangler.toml: compatibility_flags = ["nodejs_compat"] npx wrangler deploy --dry-run --outdir=out # bundles fine, 232 KiB npx wrangler dev --local # worker throws at load, no server ``` The same project installed from **JSR** boots and serves: ```bash npx jsr add @green-tea/core # -> "npm:@jsr/green-tea__core@..." npx wrangler dev --local curl -i localhost:8787/ # 200, with the security headers ``` JSR publishes transpiled source with no bundler and therefore no banner, which is why that path is unaffected — and which confirms the banner is the whole cause rather than one contributor. ## Why the edge test is green `test/edge/run.ts` bundles `test/edge/worker.ts`, which imports `../../src/index` and `../../src/edge` — the **sources**. `dist/` is never loaded by any test, so the one artifact users install is the one nothing exercises. The test's own comment reasons carefully about `nodejs_compat` and `node:http`, all of it correct, and none of it reaches the banner because the banner does not exist on the path it tests. ## Suggested fix Make the banner match what `node-require.ts` already assumes — that a missing `require` is a fact to discover, not a crash: ```js const require = import.meta.url ? _createRequire(import.meta.url) : undefined; ``` `typeof require === 'function'` then fails on workerd, `resolveRequire` falls through to `process.getBuiltinModule`, that returns nothing, and `nodeRequire` throws only when something actually calls it — which is the designed behaviour, and which `edgeHandler` never does. Worth pairing with a test that loads `dist/edge.mjs` rather than `src/edge.ts`. Bundling the built artifact is the only way this class of defect is visible, and it is a packaging problem in the same family as #69 (the JSR `createRequire` crash) — that one was source-only breaking, this one is build-only breaking, and the test suite can currently see neither from the other side. Found while adding a `--runtime edge` target to the [matcha](https://github.com/Expressive-Tea/matcha) CLI. That scaffold installs core from JSR for now and says why in its `wrangler.toml`; it can go back to a plain npm dependency once this is fixed.
Author
Owner

Fixed in 26.11.0-rc.1 (b7a852a). From the changelog:

  • The npm ESM build loads on Cloudflare Workers (#93). tsup gives every ESM file a createRequire(import.meta.url) banner so the lazy require('ws')/require('busboy') work, and it ran at module scope. On workerd import.meta.url is not a file URL, so createRequire threw and the whole worker failed to load: @green-tea/core/edge from npm could not be used at all. The edge suite never noticed because it bundles src/. The call is now guarded; where it fails, require is left undefined and the existing fallback takes over. npm run test:edge now also builds and boots the published dist/ on workerd. Node, Deno, Bun and JSR are unchanged.
Fixed in **26.11.0-rc.1** (`b7a852a`). From the changelog: - **The npm ESM build loads on Cloudflare Workers** (#93). tsup gives every ESM file a `createRequire(import.meta.url)` banner so the lazy `require('ws')`/`require('busboy')` work, and it ran at module scope. On workerd `import.meta.url` is not a file URL, so `createRequire` threw and the whole worker failed to load: `@green-tea/core/edge` from npm could not be used at all. The edge suite never noticed because it bundles `src/`. The call is now guarded; where it fails, `require` is left undefined and the existing fallback takes over. `npm run test:edge` now also builds and boots the published `dist/` on workerd. Node, Deno, Bun and JSR are unchanged.
chrnx closed this issue 2026-11-18 01:03:57 +00:00
Sign in to join this conversation.