Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 33c0562

Browse files
committed
Add more detailed suggestion when importing Node built-in modules
1 parent fba1e57 commit 33c0562

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

packages/runner-vm/src/linker.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { readFileSync } from "fs";
22
import fs from "fs/promises";
3+
import { builtinModules } from "module";
34
import path from "path";
45
import vm from "vm";
56
import {
@@ -14,6 +15,13 @@ import { VMScriptRunnerError } from "./error";
1415
const SUGGEST_BUNDLE =
1516
"If you're trying to import an npm package, you'll need to bundle your Worker first.";
1617

18+
const SUGGEST_NODE =
19+
"If you're trying to import a Node.js built-in module, or an npm package " +
20+
"that uses Node.js built-ins, you'll either need to:" +
21+
"\n- Bundle your Worker, configuring your bundler to polyfill Node.js built-ins" +
22+
"\n- Configure your bundler to load Workers-compatible builds by changing the main fields/conditions" +
23+
"\n- Find an alternative package that doesn't require Node.js built-ins";
24+
1725
interface CommonJSModule {
1826
exports: any;
1927
}
@@ -87,9 +95,11 @@ export class ModuleLinker {
8795
rule.include.test(relativeIdentifier)
8896
);
8997
if (rule === undefined) {
98+
const isBuiltin = builtinModules.includes(spec);
99+
const suggestion = isBuiltin ? SUGGEST_NODE : SUGGEST_BUNDLE;
90100
throw new VMScriptRunnerError(
91101
"ERR_MODULE_RULE",
92-
`${errorBase}: no matching module rules.\n${SUGGEST_BUNDLE}`
102+
`${errorBase}: no matching module rules.\n${suggestion}`
93103
);
94104
}
95105

@@ -179,9 +189,11 @@ export class ModuleLinker {
179189
rule.include.test(relativeIdentifier)
180190
);
181191
if (rule === undefined) {
192+
const isBuiltin = builtinModules.includes(spec);
193+
const suggestion = isBuiltin ? SUGGEST_NODE : SUGGEST_BUNDLE;
182194
throw new VMScriptRunnerError(
183195
"ERR_MODULE_RULE",
184-
`${errorBase}: no matching module rules.\n${SUGGEST_BUNDLE}`
196+
`${errorBase}: no matching module rules.\n${suggestion}`
185197
);
186198
}
187199

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require("path");

packages/runner-vm/test/linker.spec.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,16 @@ test("ModuleLinker: throws error if no matching module rule via ES module", asyn
110110
await t.throwsAsync(result, {
111111
instanceOf: VMScriptRunnerError,
112112
code: "ERR_MODULE_RULE",
113-
message: /no matching module rules/,
113+
message: /no matching module rules.*\nIf you're trying to import an npm/,
114+
});
115+
});
116+
test("ModuleLinker: throws error for Node built-in module via ES module", async (t) => {
117+
const result = run(`import fs from "fs"`);
118+
await t.throwsAsync(result, {
119+
instanceOf: VMScriptRunnerError,
120+
code: "ERR_MODULE_RULE",
121+
message:
122+
/no matching module rules.*\nIf you're trying to import a Node\.js built-in/,
114123
});
115124
});
116125
test("ModuleLinker: throws error for unsupported module type via ES module", async (t) => {
@@ -181,7 +190,18 @@ test("ModuleLinker: throws error if no matching module rule via CommonJS module"
181190
await t.throwsAsync(result, {
182191
instanceOf: VMScriptRunnerError,
183192
code: "ERR_MODULE_RULE",
184-
message: /no matching module rules/,
193+
message: /no matching module rules.*\nIf you're trying to import an npm/,
194+
});
195+
});
196+
test("ModuleLinker: throws error for Node built-in module via CommonJS module", async (t) => {
197+
const result = run(
198+
`import value from "./cjsbuiltin.cjs"; export default value;`
199+
);
200+
await t.throwsAsync(result, {
201+
instanceOf: VMScriptRunnerError,
202+
code: "ERR_MODULE_RULE",
203+
message:
204+
/no matching module rules.*\nIf you're trying to import a Node\.js built-in/,
185205
});
186206
});
187207
test("ModuleLinker: throws error for unsupported module type via CommonJS module", async (t) => {

0 commit comments

Comments
 (0)