Sharing Code Between Webpack Builds With Module Federation
Webpack 5 Module Federation lets independently deployed builds share modules at runtime, but it shifts failures from build time to runtime.
25 Dec 2025, 08:43 UTC

The problem: two Reacts, one page
Your checkout page loads, then crashes with "Invalid hook call". The product team just deployed a new version of their micro-frontend, and their bundle ships React 19 while your host app bundles React 18. Both copies load in the browser, and React hooks break when state crosses the boundary.
Webpack 5 Module Federation addresses this by letting independently deployed builds import each other's modules at runtime. Instead of publishing a shared library to a registry and versioning it in every package, a remote build exposes modules through a remoteEntry.js file, and a host build consumes them like ordinary imports.
The practical takeaway is that federation removes the build-time wall between applications, but it moves failure modes to runtime. A missing remote entry or a mismatched dependency version will not fail your CI; it will fail in the browser.
What changes across the build boundary
Three concepts define the relationship:
- Host: the consuming build that lists remotes and imports exposed modules.
- Remote: the producing build that exposes modules and serves
remoteEntry.js. - Shared dependencies: libraries such as React declared with
singleton: trueso only one copy is loaded, even when host and remote both request them.
A remote entry is a small generated file that tells the host where to find exposed modules. When the host imports products/ProductCard, Webpack fetches the remote entry, then loads the module code from the remote origin.
Worked example: host and remote configs
Assume two separate Webpack 5 projects. First confirm the toolchain:
# Run in each project root. No special permissions required.
webpack --version
Expected output begins with 5.. If it returns 4.x, the Module Federation plugin syntax below will not apply.
Remote build (webpack.remote.js):
const { ModuleFederationPlugin } = require('webpack').container;
module.exports = {
name: 'products',
filename: 'remoteEntry.js',
exposes: {
'./ProductCard': './src/ProductCard',
},
shared: ['react', 'react-dom'],
};
Host build (webpack.host.js):
const { ModuleFederationPlugin } = require('webpack').container;
module.exports = {
plugins: [
new ModuleFederationPlugin({
name: 'checkout',
remotes: {
products: 'products@http://localhost:3001/remoteEntry.js',
},
shared: {
react: { singleton: true, requiredVersion: '^18.2.0' },
'react-dom': { singleton: true, requiredVersion: '^18.2.0' },
},
}),
],
};
In the host source, import the exposed module:
import ProductCard from 'products/ProductCard';
// or, for lazy loading:
const ProductCard = React.lazy(() => import('products/ProductCard'));
Build and serve both projects on different ports:
# Remote terminal
npx webpack serve --config webpack.remote.js --port 3001
# Host terminal
npx webpack serve --config webpack.host.js --port 3000
Risk: if the remote server is down when the host loads, the remote entry request fails and the host application can break. Use React error boundaries or a dynamic-import fallback around federated code.
Verify the runtime boundary
Open the host page in a browser and open DevTools:
- Network tab, filter by
remoteEntry. Confirm the file returns HTTP 200. - Network tab, filter by
react. Confirm only one copy of React is requested across host and remote. - Console tab. Confirm there are no chunk-load errors such as "Loading chunk ... failed".
If you see two copies of React, the singleton failed. Check that requiredVersion matches the installed version in both builds, and confirm neither build is bundling a private copy of React outside the shared key.
Limitations and rollback
- Runtime failure: a remote outage can break the host. Mitigate with error boundaries and fallback UIs.
- Version drift:
singletondoes not guarantee compatibility. A remote built against React 19 can still misbehave when forced to use React 18. - Debugging: stack traces and source maps may not fully reconstruct across federation boundaries.
Because editing webpack.host.js changes build state, rollback means restoring the previous configuration file and redeploying the prior build artifacts. There is no database migration to reverse.
Start small, pin versions
Expose one shared component first, not your entire application. Pin shared dependencies to the same major version in host and remote, and treat the remote entry URL as a runtime contract between teams. Federation solves distribution; it does not replace communication about breaking changes.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.