core icon indicating copy to clipboard operation
core copied to clipboard

Error "remoteEntryExports is undefined" when loading a remote application in a Pure ESM Project

Open hoo00nn opened this issue 9 months ago • 3 comments

Describe the bug

Hello. First, I would like to thank you for providing such a great library, which has been a tremendous help in building MFA.

What I am trying to achieve in my project is to integrate a remote application at runtime during the SSR process to generate HTML.

I am using NestJS as the SSR server. Everything seems to be working fine, but I encountered one issue. In a NestJS project configured with pure ESM, an error occurs when trying to load a remote application via loadRemote provided by @module-federation/runtime

Error: [ Federation Runtime ]: remoteEntryExports is undefined 
{
  "name": "myapp",
  "entry": "http://localhost:3004/server/remoteEntry.js",
  "shareScope": "default",
  "type": "global",
  "entryGlobalName": "myapp"
}

The host application is a pure ESM-configured NestJS application, and the remote application exposes React components using Webpack.

Are there any considerations for using the runtime API in an ESM-configured application when dealing with remotes? I have confirmed that it works correctly in a NestJS application using the CJS module format.

I have created a small repository for testing. The repository contains a NestJS application in ESM module format, a NestJS application in CJS format, and a remote application. You can experience the error when running the server in ESM.

From my investigation, it seems the error is triggered when executing this code

I would appreciate any help on this issue.

Reproduction

https://github.com/hoo00nn/module-federation-esm

Used Package Manager

pnpm

System Info

System:
OS: macOS 14.1
CPU: (12) arm64 Apple M3 Pro
Memory: 31.67 MB / 18.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 20.14.0 - ~/.nvm/versions/node/v20.14.0/bin/node
Yarn: 1.22.21 - /opt/homebrew/bin/yarn
npm: 10.7.0 - ~/.nvm/versions/node/v20.14.0/bin/npm
pnpm: 8.15.7 - ~/.nvm/versions/node/v20.14.0/bin/pnpm
Browsers:
Chrome: 125.0.6422.77
Safari: 17.1

Validations

hoo00nn avatar Feb 28 '25 03:02 hoo00nn

ESM wont work on servers, SSR has to consume commonjs remote.

ScriptedAlchemy avatar Mar 11 '25 17:03 ScriptedAlchemy

ESM wont work on servers, SSR has to consume commonjs remote.

@ScriptedAlchemy Thank you for your response. There are a few points I would like to clarify.

I understand that it is possible to load CommonJS modules in an ESM environment. When you mentioned that ESM cannot be used on the server, were you referring specifically to using it with @module-federation/runtime?

Currently, the Remote App is built using CommonJS. In order to use it, does the NestJS server responsible for SSR also need to be configured as CommonJS?

hoo00nn avatar Mar 11 '25 18:03 hoo00nn

It depends. I haven't personally tried this. But if you use import to load them, then there's no way to release the require cache to reload the server with updated remotes in ESM. If you use loadRemote, then it should be fine as long as the code you're loading works in eval-like cases. You can't eval ESM because it's not a script, so it throws a syntax error.

We use vm.runInThisContext to execute federation remotes. That means it will want scripts, not modules. The host should be fine in ESM as long as you don't need to refresh the server or don't use import from, which requires us to clear the require cache to fetch new chunks when remotes are updated.

ScriptedAlchemy avatar Mar 15 '25 09:03 ScriptedAlchemy

Stale issue message

github-actions[bot] avatar May 14 '25 15:05 github-actions[bot]