Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: recommend node compat on builtin use #1027

Merged
merged 7 commits into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tricky-actors-pretend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": minor
rozenmd marked this conversation as resolved.
Show resolved Hide resolved
---

feat: trying to use node builtins should recommend you enable node_compat in wrangler.toml
8 changes: 8 additions & 0 deletions examples/workers-node-app/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import process from "process";

export default {
async fetch(): Promise<Response> {
console.log("process.env: ", process.env);
return new Response("Hello World!");
},
};
5 changes: 5 additions & 0 deletions examples/workers-node-app/wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name = "workers-node-app"
compatibility_date = "2022-05-16"
main = "src/index.ts"

node_compat = true
29 changes: 28 additions & 1 deletion packages/wrangler/src/bundle.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import assert from "node:assert";
import * as fs from "node:fs";
import { builtinModules } from "node:module";
import * as path from "node:path";
import NodeGlobalsPolyfills from "@esbuild-plugins/node-globals-polyfill";
import NodeModulesPolyfills from "@esbuild-plugins/node-modules-polyfill";
Expand Down Expand Up @@ -60,6 +61,30 @@ export async function bundleWorker(
format: entry.format,
rules,
});
const checkForNodeBuiltinsPlugin = {
rozenmd marked this conversation as resolved.
Show resolved Hide resolved
rozenmd marked this conversation as resolved.
Show resolved Hide resolved
name: "checkForNodeBuiltins",
setup(build: esbuild.PluginBuild) {
build.onResolve(
// filter out regular node builtin modules
// and the newer "node:<MODULE>" format
{
filter: new RegExp(
"^(" +
[...builtinModules].join("|") +
rozenmd marked this conversation as resolved.
Show resolved Hide resolved
"|" +
[...builtinModules].map((module) => "node:" + module).join("|") +
")$"
),
},
() => {
throw new Error(
`Detected a Node builtin module import while Node compatibility is disabled.\nAdd node_compat = true to your wrangler.toml file to enable Node compatibility.`
);
}
);
},
};

const result = await esbuild.build({
...getEntryPoint(entry.file, serveAssetsFromWorker),
bundle: true,
Expand Down Expand Up @@ -87,7 +112,9 @@ export async function bundleWorker(
moduleCollector.plugin,
...(nodeCompat
? [NodeGlobalsPolyfills({ buffer: true }), NodeModulesPolyfills()]
: []),
: // we use checkForNodeBuiltinsPlugin to throw a nicer error
// if we find node builtins when nodeCompat isn't turned on
[checkForNodeBuiltinsPlugin]),
],
...(jsxFactory && { jsxFactory }),
...(jsxFragment && { jsxFragment }),
Expand Down