cloudflare / cloudflare/workerd

🐛 Bug Report — Runtime APIs: `require()` of Node built-in module in `nodeJsCompatModule` outside virtual root throws

Open
#1,101 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 \"./dir/index.cjs\"",
),
( name = "dir/index.cjs",
nodeJsCompatModule =
`const { Buffer } = require("buffer");
`module.exports = {
` async fetch(request, env, ctx) {
` return new Response(Buffer.from("test").toString());
` }
`}
),
],
);
```

...`require("buffer")` throws with `Error: No such module "dir/node:buffer".`. Replacing it with `require("node:buffer")` also exhibits the same error. Replacing `dir/index.cjs` with `index.cjs` fixes the issue.

Contributor guide

Open the contributing guide

Research direction

Start by running the supplied workerd configuration and reproducing the failure from the nested nodeJsCompatModule. Trace how require("buffer") and require("node:buffer") are resolved relative to dir/index.cjs; done means both built-in imports resolve there as they do when the module is at the virtual root.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.