Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Module Federation fails with: Module does not exist in container

ErrorModule "./Button" does not exist in container

Short answer

The host asked a remote container for a key it does not list. Check the exposes key first: it must start with './', and the host must ask for exactly that key. Then check that the remote name in the host matches the remote's own name, and that a dynamically loaded container was loaded and initialised.

  • Applies to webpack 5, Module Federation, React 18, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Open the remote's remoteEntry.js address in the browser or with curl, then compare the exposes keys in the remote config with the import in the host.

    curl -I http://localhost:3001/remoteEntry.js
    grep -n -A4 "exposes" webpack.config.js
  2. 2Match what you see, then jump to its fix

    1. 1The message names a key such as ./ButtonThe exposes key does not start with ./Fix
    2. 2The script loads but the global named before the @ never appears, or ScriptExternalLoadErrorThe remote name in the host does not match the remote's nameFix
    3. 3container is undefined, or fn is not a function, when you call getA dynamically loaded container was used before it was loaded or initialisedFix

What does the error look like?

  • Module "./Button" does not exist in container
  • Uncaught TypeError: fn is not a function

Cause 1 of 3

The exposes key does not start with ./

1Check

The webpack troubleshooting section for this exact message shows the key written as 'Button' and the fixed form './Button'.

grep -n -A4 "exposes" webpack.config.js

2Fix

Write each key with a leading ./ and import it from the host as remoteName/Button.

new ModuleFederationPlugin({
  name: 'app1',
  exposes: {
    './Button': './src/Button',
  },
})

// host
import Button from 'app1/Button';

3Verify

Rebuild the remote and reload the host; the component renders.

curl -s http://localhost:3001/remoteEntry.js | head -c 200

Confirmed by webpack.js.org

Did this fix it?

Cause 2 of 3

The remote name in the host does not match the remote's name

1Check

In remotes the entry is written name@url. Webpack waits for the global named before the @ to appear, so it has to equal the name given to the plugin in the remote build.

grep -n "name:" webpack.config.js
grep -n -A3 "remotes" webpack.config.js

2Fix

Make the key and the part before @ in the host the same as name in the remote.

// host
remotes: {
  app1: 'app1@http://localhost:3001/remoteEntry.js',
}

3Verify

Reload the host with the network panel open and see remoteEntry.js load and the import resolve.

curl -I http://localhost:3001/remoteEntry.js

Confirmed by webpack.js.org

Did this fix it?

Cause 3 of 3

A dynamically loaded container was used before it was loaded or initialised

1Check

When you load the script yourself, the container exists only after the script has finished loading, and it needs the shared scope before get is called.

grep -rn "container.init\|__webpack_init_sharing__" src/

2Fix

Wait for the script, initialise sharing, call init once, then get the module.

await __webpack_init_sharing__('default');
await container.init(__webpack_share_scopes__.default);
const factory = await container.get('./Button');
const Module = factory();

3Verify

The module resolves without an error in the console.

// open the browser console on the host page

Confirmed by webpack.js.org

Did this fix it?

How do I stop it coming back?

  • Keep exposes keys and host imports in one shared list so a rename shows up in both repos.
  • Open remoteEntry.js directly after every deploy of a remote.
  • Load each remote once and keep the result.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.