@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:
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:
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.
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.
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
@green-tea/core/edgeinstalled from npm does not load on workerd. Theworker throws before any green-tea code runs:
Cause
tsup.config.tsgives the ESM build acreateRequirebanner:That runs at module scope, in every ESM entry point including
dist/edge.mjs,and
import.meta.urlisundefinedon workerd. From the wrangler bundle:src/node-require.tsis written to make exactly this safe — it resolves lazily,prefers an ambient
require, and its doc comment says workerd is the runtimewhere 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
createRequireeagerly, at load, for a code path (
edgeHandler) that never requires anything.Reproduction
The same project installed from JSR boots and serves:
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.tsbundlestest/edge/worker.ts, which imports../../src/indexand
../../src/edge— the sources.dist/is never loaded by any test, sothe one artifact users install is the one nothing exercises. The test's own
comment reasons carefully about
nodejs_compatandnode:http, all of itcorrect, 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.tsalready assumes — that a missingrequireis a fact to discover, not a crash:typeof require === 'function'then fails on workerd,resolveRequirefallsthrough to
process.getBuiltinModule, that returns nothing, andnodeRequirethrows only when something actually calls it — which is the designed behaviour,
and which
edgeHandlernever does.Worth pairing with a test that loads
dist/edge.mjsrather thansrc/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
createRequirecrash) — 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 edgetarget to thematcha CLI. That scaffold installs
core from JSR for now and says why in its
wrangler.toml; it can go back to aplain npm dependency once this is fixed.
Fixed in 26.11.0-rc.1 (
b7a852a). From the changelog:createRequire(import.meta.url)banner so the lazyrequire('ws')/require('busboy')work, and it ran at module scope. On workerdimport.meta.urlis not a file URL, socreateRequirethrew and the whole worker failed to load:@green-tea/core/edgefrom npm could not be used at all. The edge suite never noticed because it bundlessrc/. The call is now guarded; where it fails,requireis left undefined and the existing fallback takes over.npm run test:edgenow also builds and boots the publisheddist/on workerd. Node, Deno, Bun and JSR are unchanged.