Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions .changeset/native-modules-host-node.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": patch
---

Fix bundling of `node:_stream_wrap` with `nodejs_compat` when running Wrangler on Node.js 26

Node.js 26 no longer ships `_stream_wrap` as a built-in module, so Wrangler failed to build Workers that import it with `Could not resolve "node:_stream_wrap"`, even though the Workers runtime provides the module natively. Such Workers now build on every supported Node.js version.
63 changes: 63 additions & 0 deletions packages/wrangler/src/__tests__/deploy/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,69 @@ export default { fetch() { return new Response(foo); } }`
`import path from "node:path";`
);
});

// Node.js 26 removed `_stream_wrap`, but workerd still provides it natively.
// Externals must not depend on the built-ins of the Node.js version running Wrangler.
it(`when present, should support "external" native modules that the host Node.js does not provide`, async ({
Comment thread
NuroDev marked this conversation as resolved.
Resolved
expect,
}) => {
writeWranglerConfig({
compatibility_date: "2026-01-29",
});
fs.writeFileSync(
"index.js",
`
import prefixed from 'node:_stream_wrap';
import nonPrefixed from '_stream_wrap';
console.log(prefixed, nonPrefixed);
export default {}
`
);

await runWrangler(
"deploy index.js --dry-run --outdir=dist --compatibility-flag=nodejs_compat"
);

const output = fs.readFileSync("dist/index.js", { encoding: "utf-8" });
expect(output).toContain(`import prefixed from "node:_stream_wrap";`);
expect(output).toContain(`import nonPrefixed from "_stream_wrap";`);
});

// `sqlite` only exists as `node:sqlite`, so a bare `sqlite` import refers to an npm package.
it(`when present, should bundle npm packages that share a name with prefix-only native modules`, async ({
expect,
}) => {
writeWranglerConfig({
compatibility_date: "2026-01-29",
});
fs.mkdirSync("node_modules/sqlite", { recursive: true });
fs.writeFileSync(
"node_modules/sqlite/package.json",
JSON.stringify({ name: "sqlite", main: "index.js" })
);
Comment thread
NuroDev marked this conversation as resolved.
Resolved
fs.writeFileSync(
"node_modules/sqlite/index.js",
`module.exports = "npm sqlite package";`
);
fs.writeFileSync(
"index.js",
`
import native from 'node:sqlite';
import npmPackage from 'sqlite';
console.log(native, npmPackage);
export default {}
`
);

await runWrangler(
"deploy index.js --dry-run --outdir=dist --compatibility-flag=nodejs_compat"
);

const output = fs.readFileSync("dist/index.js", { encoding: "utf-8" });
expect(output).toContain(`import native from "node:sqlite";`);
expect(output).toContain(`"npm sqlite package"`);
expect(output).not.toContain(`from "sqlite"`);
});
});
describe("bundle reporter", () => {
it("should print the bundle size", async ({ expect }) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ export function nodejsHybridPlugin({

errorOnServiceWorkerFormat(build, nodeJsModuleRegexp);
handleRequireCallsToNodeJSBuiltins(build, nodeJsModuleRegexp);
handleUnenvAliasedPackages(build, alias, external);
handleUnenvAliasedPackages(
build,
alias,
external,
nonPrefixedNodeModules
);
handleNodeJSGlobals(build, inject, polyfill);
},
};
Expand Down Expand Up @@ -138,15 +143,30 @@ function handleRequireCallsToNodeJSBuiltins(
* @param build ESBuild PluginBuild.
* @param alias Aliases resolved to absolute paths.
* @param external external modules.
* @param nonPrefixedNodeModules Node.js built-in modules importable without the `node:` prefix.
*/
function handleUnenvAliasedPackages(
build: PluginBuild,
alias: Record<string, string>,
external: readonly string[]
external: readonly string[],
nonPrefixedNodeModules: readonly string[]
) {
// esbuild expects alias paths to be absolute
const aliasAbsolute: Record<string, string> = {};
// Aliases to native modules that workerd provides, left as external imports
const nativeAliases = new Set<string>();
for (const [module, unresolvedAlias] of Object.entries(alias)) {
// Native Node.js modules are provided by workerd at runtime, so they must not be resolved
// against the host Node.js, which may not provide them (e.g. `_stream_wrap` in Node.js 26).
// Bare names outside `nonPrefixedNodeModules` (e.g. `sqlite`) are npm packages, not built-ins.
const isNodeBuiltin =
unresolvedAlias.startsWith("node:") ||
nonPrefixedNodeModules.includes(unresolvedAlias);
if (isNodeBuiltin && external.includes(unresolvedAlias)) {
aliasAbsolute[module] = unresolvedAlias;
nativeAliases.add(module);
continue;
}
try {
aliasAbsolute[module] = require.resolve(unresolvedAlias);
} catch {
Expand All @@ -173,10 +193,10 @@ function handleUnenvAliasedPackages(
};
}

// Resolve the alias to its absolute path and potentially mark it as external
// Resolve the alias to its absolute path, or keep native modules as external imports
return {
path: aliasAbsolute[args.path],
external: external.includes(unresolvedAlias),
external: nativeAliases.has(args.path),
};
});

Expand Down
Loading