Skip to content

fix(sdk): handle node builtins in ESM remote loader#4816

Open
dmchoi77 wants to merge 5 commits into
module-federation:mainfrom
dmchoi77:fix/node-esm-builtin-loader
Open

fix(sdk): handle node builtins in ESM remote loader#4816
dmchoi77 wants to merge 5 commits into
module-federation:mainfrom
dmchoi77:fix/node-esm-builtin-loader

Conversation

@dmchoi77

Copy link
Copy Markdown

Description

Handle Node.js built-in module imports in the SDK Node ESM remote loader.

This PR updates the vm.SourceTextModule loading path used by loadScriptNode(..., { type: 'module' }) so that:

  • Node builtin specifiers such as node:url, node:module, and bare builtins such as path are loaded with native Node import and returned to the linker as vm.SyntheticModule instances.
  • ESM remote module loading only fetches http: and https: module URLs; unsupported non-http specifiers fail with a clear error instead of being routed through fetch.
  • Remote ESM modules receive a synthetic import.meta.url, which allows patterns such as createRequire(import.meta.url).
  • Dynamically imported ESM chunks are evaluated before their namespace is consumed.

The main issue this PR is meant to address is the node:* builtin path. The import.meta.url and dynamic import pieces are adjacent ESM loader fixes found while covering the same Node ESM remote loader flow. I opened this as a draft so maintainers can confirm whether they prefer keeping those together or splitting them.

Related Issue

Related #4815

Types of changes

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

Checklist

  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • I have updated the documentation.

Test Plan

  • NODE_OPTIONS=--experimental-vm-modules corepack pnpm --filter @module-federation/sdk test
  • corepack pnpm --filter @module-federation/sdk run lint
  • corepack pnpm --filter @module-federation/sdk run build

Note: build completes successfully, with an existing rolldown warning about the pre-existing eval('require') CJS path in packages/sdk/src/node.ts.

@changeset-bot

changeset-bot Bot commented Jun 14, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: a8e5ff2

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 46 packages
Name Type
@module-federation/sdk Patch
@module-federation/devtools Patch
@module-federation/cli Patch
@module-federation/dts-plugin Patch
@module-federation/enhanced Patch
@module-federation/esbuild Patch
@module-federation/managers Patch
@module-federation/manifest Patch
@module-federation/metro Patch
@module-federation/modern-js-v3 Patch
@module-federation/modern-js Patch
@module-federation/nextjs-mf Patch
@module-federation/node Patch
@module-federation/observability-plugin Patch
@module-federation/retry-plugin Patch
@module-federation/rsbuild-plugin Patch
@module-federation/rspack Patch
@module-federation/rspress-plugin Patch
@module-federation/runtime-core Patch
@module-federation/runtime Patch
@module-federation/storybook-addon Patch
@module-federation/utilities Patch
@module-federation/webpack-bundler-runtime Patch
@module-federation/bridge-react-webpack-plugin Patch
@module-federation/bridge-react Patch
@module-federation/bridge-vue3 Patch
shared-tree-shaking-no-server-host Patch
shared-tree-shaking-no-server-provider Patch
@module-federation/metro-plugin-rnc-cli Patch
@module-federation/metro-plugin-rnef Patch
@module-federation/metro-plugin-rock Patch
shared-tree-shaking-with-server-host Patch
shared-tree-shaking-with-server-provider Patch
node-dynamic-remote-new-version Patch
node-dynamic-remote Patch
remote5 Patch
remote6 Patch
website-new Patch
@module-federation/runtime-tools Patch
@module-federation/inject-external-runtime-core-plugin Patch
@module-federation/third-party-dts-extractor Patch
@module-federation/bridge-shared Patch
@module-federation/error-codes Patch
create-module-federation Patch
@module-federation/treeshake-server Patch
@module-federation/treeshake-frontend Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@netlify

netlify Bot commented Jun 14, 2026

Copy link
Copy Markdown

Deploy Preview for module-federation-docs ready!

Name Link
🔨 Latest commit a8e5ff2
🔍 Latest deploy log https://app.netlify.com/projects/module-federation-docs/deploys/6a3a7cfce7a1470008ae1c88
😎 Deploy Preview https://deploy-preview-4816--module-federation-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@2heal1

2heal1 commented Jun 22, 2026

Copy link
Copy Markdown
Member

Thanks for opening the draft PR. This looks like it may address the issue. Since it is still in draft, we'll wait until you mark it as ready for review, then take a closer look.

@dmchoi77 dmchoi77 marked this pull request as ready for review June 22, 2026 06:26
@dmchoi77

Copy link
Copy Markdown
Author

@2heal1 Thank you for your response. I’ve marked the PR as ready for review. Please take a look when you have a chance.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5fc8b29b22

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/sdk/src/node.ts
Comment on lines +280 to +281
function createImportMetaUrl(url: string): string {
return `file:///__module_federation_remote__${encodeRemoteModulePath(url)}`;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Base synthetic import.meta.url where require can resolve

When an HTTP remote calls createRequire(import.meta.url) and then requires any non-builtin dependency, this fake file:///__module_federation_remote__/... location makes Node search from /__module_federation_remote__ and then /node_modules, not from the consuming app's project. That means bundled ESM remotes with externalized packages such as require('react') still fail even though the CJS path above intentionally bases createRequire on process.cwd() for non-file remotes.

Useful? React with 👍 / 👎.

globalThis.fetch = originalFetch;
});

it('loads node: builtin imports without fetching them as remote chunks', async () => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Make the new SDK spec discoverable by Jest

This new spec is not exercised by pnpm --filter @module-federation/sdk test: I checked packages/sdk/jest.config.cjs, and its testMatch is <rootDir>__tests__/**/**.spec.[jt]s?(x), which expands to packages/sdk__tests__ rather than this packages/sdk/__tests__ directory; because the package script also uses --passWithNoTests, CI can pass without running these regression cases.

Useful? React with 👍 / 👎.

@pkg-pr-new

pkg-pr-new Bot commented Jun 23, 2026

Copy link
Copy Markdown

Open in StackBlitz

@module-federation/devtools

pnpm add https://pkg.pr.new/@module-federation/devtools@2ff9f11

@module-federation/cli

pnpm add https://pkg.pr.new/@module-federation/cli@2ff9f11

create-module-federation

pnpm add https://pkg.pr.new/create-module-federation@2ff9f11

@module-federation/dts-plugin

pnpm add https://pkg.pr.new/@module-federation/dts-plugin@2ff9f11

@module-federation/enhanced

pnpm add https://pkg.pr.new/@module-federation/enhanced@2ff9f11

@module-federation/error-codes

pnpm add https://pkg.pr.new/@module-federation/error-codes@2ff9f11

@module-federation/esbuild

pnpm add https://pkg.pr.new/@module-federation/esbuild@2ff9f11

@module-federation/managers

pnpm add https://pkg.pr.new/@module-federation/managers@2ff9f11

@module-federation/manifest

pnpm add https://pkg.pr.new/@module-federation/manifest@2ff9f11

@module-federation/metro

pnpm add https://pkg.pr.new/@module-federation/metro@2ff9f11

@module-federation/metro-plugin-rnc-cli

pnpm add https://pkg.pr.new/@module-federation/metro-plugin-rnc-cli@2ff9f11

@module-federation/metro-plugin-rnef

pnpm add https://pkg.pr.new/@module-federation/metro-plugin-rnef@2ff9f11

@module-federation/metro-plugin-rock

pnpm add https://pkg.pr.new/@module-federation/metro-plugin-rock@2ff9f11

@module-federation/modern-js

pnpm add https://pkg.pr.new/@module-federation/modern-js@2ff9f11

@module-federation/modern-js-v3

pnpm add https://pkg.pr.new/@module-federation/modern-js-v3@2ff9f11

@module-federation/native-federation-tests

pnpm add https://pkg.pr.new/@module-federation/native-federation-tests@2ff9f11

@module-federation/native-federation-typescript

pnpm add https://pkg.pr.new/@module-federation/native-federation-typescript@2ff9f11

@module-federation/nextjs-mf

pnpm add https://pkg.pr.new/@module-federation/nextjs-mf@2ff9f11

@module-federation/node

pnpm add https://pkg.pr.new/@module-federation/node@2ff9f11

@module-federation/observability-plugin

pnpm add https://pkg.pr.new/@module-federation/observability-plugin@2ff9f11

@module-federation/retry-plugin

pnpm add https://pkg.pr.new/@module-federation/retry-plugin@2ff9f11

@module-federation/rsbuild-plugin

pnpm add https://pkg.pr.new/@module-federation/rsbuild-plugin@2ff9f11

@module-federation/rspack

pnpm add https://pkg.pr.new/@module-federation/rspack@2ff9f11

@module-federation/rspress-plugin

pnpm add https://pkg.pr.new/@module-federation/rspress-plugin@2ff9f11

@module-federation/runtime

pnpm add https://pkg.pr.new/@module-federation/runtime@2ff9f11

@module-federation/runtime-core

pnpm add https://pkg.pr.new/@module-federation/runtime-core@2ff9f11

@module-federation/runtime-tools

pnpm add https://pkg.pr.new/@module-federation/runtime-tools@2ff9f11

@module-federation/sdk

pnpm add https://pkg.pr.new/@module-federation/sdk@2ff9f11

@module-federation/storybook-addon

pnpm add https://pkg.pr.new/@module-federation/storybook-addon@2ff9f11

@module-federation/third-party-dts-extractor

pnpm add https://pkg.pr.new/@module-federation/third-party-dts-extractor@2ff9f11

@module-federation/treeshake-frontend

pnpm add https://pkg.pr.new/@module-federation/treeshake-frontend@2ff9f11

@module-federation/treeshake-server

pnpm add https://pkg.pr.new/@module-federation/treeshake-server@2ff9f11

@module-federation/typescript

pnpm add https://pkg.pr.new/@module-federation/typescript@2ff9f11

@module-federation/utilities

pnpm add https://pkg.pr.new/@module-federation/utilities@2ff9f11

@module-federation/webpack-bundler-runtime

pnpm add https://pkg.pr.new/@module-federation/webpack-bundler-runtime@2ff9f11

@module-federation/bridge-react

pnpm add https://pkg.pr.new/@module-federation/bridge-react@2ff9f11

@module-federation/bridge-react-webpack-plugin

pnpm add https://pkg.pr.new/@module-federation/bridge-react-webpack-plugin@2ff9f11

@module-federation/bridge-shared

pnpm add https://pkg.pr.new/@module-federation/bridge-shared@2ff9f11

@module-federation/bridge-vue3

pnpm add https://pkg.pr.new/@module-federation/bridge-vue3@2ff9f11

@module-federation/inject-external-runtime-core-plugin

pnpm add https://pkg.pr.new/@module-federation/inject-external-runtime-core-plugin@2ff9f11

commit: 2ff9f11

@github-actions

github-actions Bot commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Bundle Size Report

16 package(s) changed, 25 unchanged.

Package dist + ESM entry

Package Total dist (raw) Delta ESM gzip Delta
@module-federation/bridge-react 383.3 kB +6.7 kB (+1.8%) 1.3 kB -1 B (-0.1%)
@module-federation/bridge-vue3 175.4 kB +3.7 kB (+2.2%) 25.6 kB +679 B (+2.7%)
@module-federation/cli 26.3 kB no change 786 B no change
@module-federation/core 39.1 kB no change 173 B no change
@module-federation/devtools 664.0 kB no change 4.0 kB no change
@module-federation/enhanced 810.0 kB no change 672 B no change
@module-federation/inject-external-runtime-core-plugin 5.2 kB +26 B (+0.5%) 520 B no change
@module-federation/managers 69.8 kB no change 334 B no change
@module-federation/manifest 136.1 kB no change 182 B no change
@module-federation/metro-plugin-rnc-cli 0 B no change 314 B no change
@module-federation/node 193.4 kB no change 217 B no change
@module-federation/runtime-core 287.7 kB -8 B (-0.0%) 477 B no change
@module-federation/runtime-tools 7.8 kB +65 B (+0.8%) 142 B no change
@module-federation/sdk 127.9 kB +6.6 kB (+5.4%) 785 B no change
@module-federation/storybook-addon 79.2 kB no change 100 B no change
@module-federation/utilities 110.6 kB no change 328 B no change

Bundle targets

Package Web bundle (gzip) Delta Node bundle (gzip) Delta
@module-federation/bridge-react 17.9 kB no change 18.8 kB +518 B (+2.8%)
@module-federation/bridge-vue3 19.0 kB no change 19.2 kB +491 B (+2.6%)
@module-federation/cli 2.3 kB -30 B (-1.3%) 2.3 kB -30 B (-1.3%)
@module-federation/core 1.1 kB -31 B (-2.8%) 1.0 kB -30 B (-2.8%)
@module-federation/devtools 30.2 kB -36 B (-0.1%) 30.2 kB -36 B (-0.1%)
@module-federation/enhanced 2.6 kB -43 B (-1.6%) 2.6 kB -43 B (-1.6%)
@module-federation/inject-external-runtime-core-plugin 396 B no change 396 B no change
@module-federation/managers 2.4 kB -26 B (-1.0%) 2.4 kB -26 B (-1.0%)
@module-federation/manifest 6.1 kB -39 B (-0.6%) 6.1 kB -39 B (-0.6%)
@module-federation/metro-plugin-rnc-cli 411 B -25 B (-5.7%) 411 B -25 B (-5.7%)
@module-federation/node 9.2 kB -27 B (-0.3%) 9.2 kB -27 B (-0.3%)
@module-federation/runtime-core 15.3 kB no change 15.0 kB no change
@module-federation/runtime-tools 89 B no change 89 B no change
@module-federation/sdk 4.5 kB no change 5.9 kB +532 B (+9.7%)
@module-federation/storybook-addon 1.9 kB -25 B (-1.3%) 1.7 kB -25 B (-1.4%)
@module-federation/utilities 2.6 kB -31 B (-1.2%) 2.6 kB -31 B (-1.2%)

Consumer scenarios

Scenario Web output (gzip) Delta Node output (gzip) Delta Gap (node-web) Delta
Enhanced remoteEntry 21.4 kB -17 B (-0.1%) 23.0 kB +507 B (+2.2%) +1.6 kB +524 B

Total dist (raw): 6.83 MB (+17.0 kB (+0.2%))
Total ESM gzip: 79.2 kB (+678 B (+0.8%))
Total web bundle (gzip): 207.1 kB (-313 B (-0.1%))
Total node bundle (gzip): 209.1 kB (+1.2 kB (+0.6%))
Tracked ./bundler entry gzip: 556 B (no change)
Tracked ./bundler web bundle (gzip): 4.8 kB (no change)
Tracked ./bundler node bundle (gzip): 4.8 kB (no change)

Bundle sizes are generated with rslib (Rspack). Package-root metrics preserve the historical report. Tracked subpath exports such as ./bundler are measured separately so ENV_TARGET-driven tree-shaking is visible. Bare imports are externalized to keep package-level sizes consistent, and assets are emitted as resources.

@dmchoi77 dmchoi77 requested a review from 2heal1 June 23, 2026 12:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants