DEV Community

Alex Spinov
Alex Spinov

Posted on

Webpack 5 Has a Free Module Federation Feature — Share Code Between Apps at Runtime

Webpack 5's Module Federation lets separate applications share code at runtime. No rebuilding. No npm publishing. Just live code sharing between independent apps.

What Is Module Federation?

Traditional approach: App A and App B share a component library. You publish to npm, both apps install it, rebuild, redeploy.

Module Federation: App A exposes components. App B imports them at runtime. Update App A → App B automatically gets the update. No rebuild.

What You Get for Free

Host app (consumes remote modules):

// webpack.config.js
const { ModuleFederationPlugin } = require('webpack').container;

module.exports = {
  plugins: [
    new ModuleFederationPlugin({
      name: 'host',
      remotes: {
        remoteApp: 'remoteApp@http://localhost:3001/remoteEntry.js',
      },
      shared: ['react', 'react-dom'],
    }),
  ],
};
Enter fullscreen mode Exit fullscreen mode
// Use remote component like a local import
const RemoteButton = React.lazy(() => import('remoteApp/Button'));

function App() {
  return (
    <Suspense fallback="Loading...">
      <RemoteButton onClick={() => alert('hello')} />
    </Suspense>
  );
}
Enter fullscreen mode Exit fullscreen mode

Remote app (exposes modules):

new ModuleFederationPlugin({
  name: 'remoteApp',
  filename: 'remoteEntry.js',
  exposes: {
    './Button': './src/components/Button',
    './Header': './src/components/Header',
  },
  shared: ['react', 'react-dom'],
});
Enter fullscreen mode Exit fullscreen mode

Use Cases

  • Micro-frontends — independent teams deploy independent apps that compose into one UI
  • Shared design systems — update once, all apps get the update
  • A/B testing — swap components at runtime without full redeployment
  • Plugin architecture — load third-party extensions dynamically

Shared Dependencies

Module Federation deduplicates shared dependencies. If both apps use React 18, only one copy loads. Version mismatches? Each gets its own copy (no crashes).

Beyond Webpack 5

  • Webpack 5 — original Module Federation implementation
  • Module Federation 2.0 — framework-agnostic, works with Rspack and Vite
  • Rspack — Rust-based webpack-compatible bundler (10x faster)

If you're building micro-frontends or sharing components between apps — Module Federation eliminates the npm publish cycle.


Need web scraping or data extraction? Check out my tools on Apify — get structured data from any website in minutes.

Custom solution? Email spinov001@gmail.com — quote in 2 hours.

Top comments (1)

Collapse
 
nstlopez profile image
Néstor

If you're using Module Federation you should switch to @module-federation/enhanced since we don't support the bundled version ones anymore.

module-federation.io/guide/start/n...