cloudflare / cloudflare/workerd

🐛 Bug Report — Runtime APIs: `require()` of `node:*` module in `commonJsModule` throws

Open
#1,100 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
8.7k
Forks
739
Avg merge
2d 20h
Merged PRs (30d)
174

Description

Hey! 👋 With the following `workerd` configuration:

```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 = (
compatibilityDate = "2023-08-01",
compatibilityFlags = ["nodejs_compat"],
modules = [
( name = "index.mjs",
esModule = "export { default } from \"./index.cjs\"",
),
( name = "index.cjs",
commonJsModule =
`const { Buffer } = require("node:buffer");
`module.exports = {
` async fetch(request, env, ctx) {
` return new Response(Buffer.from("test").toString());
` }
`}
),
],
);
```

...the `require("node:buffer")` throws with `TypeError: Cannot use require() to import an ES Module.`. I know there is `nodeJsCompatModule`, but it feels like `node:`-prefixed `require()` should still work in regular CommonJS modules.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.