Webpack Module Federation: sharing code between independently deployed frontends
If you need to share UI components between independently deployed frontends, webpack Module Federation offers a runtime solution—but configuration trade‑offs can catch teams off guard. This post walks through a concrete host/remote setup, verification steps, and common pitfalls.
24 Apr 2026, 08:31 UTC

If you need to share UI components or libraries between two independently deployed React apps without merging codebases, webpack Module Federation offers a runtime solution—but it comes with configuration trade‑offs that catch teams off guard.
What Module Federation actually does
Module Federation (introduced in webpack 5) lets a host application consume modules exposed by remote builds at runtime. The remote application uses an exposes map to publish specific modules, while the host lists remotes in its webpack configuration and loads them through a generated runtime entry point. This means two separately built and deployed frontends can share React, lodash, or custom UI components without a monolithic compile step.
Key terms: exposes maps the modules a remote wants to share; shared controls which libraries (e.g., React) are loaded once across federated builds using singleton or requiredVersion strategies.
Concrete host and remote configuration
Below are minimal configuration snippets that illustrate the required shape. Replace placeholders with your actual paths, names, and versions.
/* webpack.remote.js (remote build) */
module.exports = {
mode: "development",
devServer: {
port: 3001,
static: { directory: __dirname + "/dist" },
allowedHosts: "all",
},
plugins: [
new ModuleFederationPlugin({
name: "remote",
filename: "remoteEntry.js",
exposes: {
"./Button": "./src/components/Button",
},
shared: {
react: { singleton: true, requiredVersion: "^18.0.0" },
"react-dom": { singleton: true, requiredVersion: "^18.0.0" },
},
}),
],
};
/* webpack.host.js (host build) */
module.exports = {
mode: "development",
devServer: {
port: 3000,
static: { directory: __dirname + "/dist" },
allowedHosts: "all",
},
plugins: [
new ModuleFederationPlugin({
name: "host",
remotes: {
remote: "remote@http://localhost:3001/remoteEntry.js",
},
shared: {
react: { singleton: true, requiredVersion: "^18.0.0" },
"react-dom": { singleton: true, requiredVersion: "^18.0.0" },
},
}),
],
};
In the remote config, exposes publishes the ./Button module. In the host config, remotes points to the remote’s entry URL, and shared ensures both builds agree on which React version is the singleton. Run each config from its project root with npx webpack serve --config webpack.remote.js and npx webpack serve --config webpack.host.js respectively; no special permissions are required beyond local development access.
Verifying the setup and avoiding common pitfalls
- Confirm the remote component renders. Open http://localhost:3000 in a browser. The host page should display the remote’s
Buttonwithout a build error. - Check shared dependency loading. Open developer tools → Network tab. Filter for
reactorreact-dom. You should see only one network request for the shared library, even when navigating between host and remote pages—thesingletonflag enforces this. - Build for production and test cross‑origin. Run
npx webpack --config webpack.remote.jsandnpx webpack --config webpack.host.jsto generate production bundles. Serve them from different origins (e.g.,localhost:3000andlocalhost:3001) and confirm the remote module loads at runtime without a 404 on the entry script.
Risks to watch: Version mismatches between host and remote shared dependencies can cause runtime errors even when both builds pass cleanly. If the remote entry script fails to load (e.g., 404 or CORS block), the host application may break unless error boundaries or fallback UI are implemented. Source maps and stack traces are harder to correlate across separately built origins, so inspect console logs carefully when debugging.
Limitations and when to look elsewhere
Module Federation works best when host and remote share a compatible webpack version (5.x or later) and can coordinate on shared library versions. If your team needs strict compile‑time type checking or jump‑to‑definition across build boundaries, you’ll need separate type packages or generated declarations, as static analysis does not cross federated boundaries. Additionally, deeply nested dependency trees may reveal version conflicts that require manual resolution in the shared configuration.
For teams evaluating micro‑frontend architectures, Module Federation is a strong starting point when the primary goal is runtime code sharing without a full build monolith. When strict version governance or shared type definitions are non‑negotiable, consider module‑barrel patterns or dedicated module federation platforms that provide stricter contract enforcement.
Next step: Scaffold two webpack 5 projects, configure one as host with a remote entry URL and the other as remote exposing a component, run both dev servers, and confirm the host renders the remote component. Use browser dev tools to verify the singleton shared dependency is fetched only once.