cloudflare / cloudflare/workerd
🐛 Bug Report — Circular `require()` not supported
- Dominant language
- C++
- Stars
- 8.7k
- Forks
- 739
- Avg merge
- 2d 20h
- Merged PRs (30d)
- 174
Description
Hey! 👋 I'm sure you're already aware of this, but want to make sure it doesn't get lost. `workerd`'s `require()` doesn't currently support cycles. This is something that came up when building a Vitest pool for Workers, as one of Vitest's dependencies `chai`, includes these. We're working around this by bundling `chai` with `esbuild` before returning it from the module fallback service, but ideally we wouldn't need to. For a simple example of something that doesn't work, but does in Node:
```capnp
using Workerd = import "/workerd/workerd.capnp";
const config :Workerd.Config = (
services = [
( name = "main", worker = .worker ),
],
sockets = [
( name = "http", address = "*:8080", http = (), service = "main" ),
]
);
const worker :Workerd.Worker = (
modules = [
( name = "index.mjs",
esModule =
`import a from "./a.cjs";
`export default {
` async fetch(request, env, ctx) {
` return new Response(String(a.three()));
` }
`}
),
( name = "a.cjs",
commonJsModule =
`const b = require("./b.cjs");
`exports.one = () => 1;
`exports.three = () => 1 + b.two();
),
( name = "b.cjs",
commonJsModule =
`const a = require("./a.cjs");
`exports.two = () => a.one() + a.one();
)
],
compatibilityDate = "2023-02-28",
);
```
Contributor guide
Assessment
This issue has not been assessed yet.