Module Federation fails with: Module does not exist in container
Module "./Button" does not exist in containerShort 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.
Fix it fast
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.js2Match what you see, then jump to its fix
- 1
The message names a key such as ./ButtonThe exposes key does not start with ./Fix - 2
The 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
container is undefined, or fn is not a function, when you call getA dynamically loaded container was used before it was loaded or initialisedFix
- 1
What does the error look like?
Module "./Button" does not exist in containerUncaught 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.js2Fix
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 200Confirmed by webpack.js.org
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.js2Fix
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.jsConfirmed by webpack.js.org
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 pageConfirmed by webpack.js.org
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Web apps
Module Federation app crashes on start with 'Shared module is not available for eager consumption'
Shared module is not available for eager consumption: webpack/sharing/consume/default/react/react
Module Federation fills the shared scope (react, react-dom) asynchronously.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fixWeb apps
Axios returns 401 from a React app but the login works elsewhere
Error: Request failed with status code 401
A 401 means the server received the request without valid credentials.
3 causes, each with a fix