Skip to main content

Lesson 4 of 8

Micro-frontends with Module Federation

Micro-frontends split a large frontend into independently deployable pieces - different teams own different sections, can use different frameworks, and deploy independently. Module Federation (Vite or Webpack) is the production solution for loading remote code at runtime.

40 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Webpack Module Federation - Host & Remote

Webpack Module Federation - Host & Remote
// ---- Remote app (Products MFE) ---- 
// packages/mfe-products/webpack.config.js
const { ModuleFederationPlugin } = require('webpack').container;

module.exports = {
  mode: 'production',
  devServer: { port: 3001 },
  plugins: [
    new ModuleFederationPlugin({
      name: 'products',                 // MUST be unique across all MFEs
      filename: 'remoteEntry.js',       // the manifest file loaded by host
      exposes: {
        // Map alias → actual file exported to other apps
        './ProductList': './src/features/products/ProductList',
        './ProductCard': './src/features/products/ProductCard',
        './useProducts': './src/features/products/useProducts',
      },
      shared: {
        react: { singleton: true, requiredVersion: '^18.0.0' },
        'react-dom': { singleton: true, requiredVersion: '^18.0.0' },
        // singleton: true ensures only ONE copy of React, even across MFEs
      },
    }),
  ],
};

// ---- Host app (Shell) ----
// apps/shell/webpack.config.js
new ModuleFederationPlugin({
  name: 'shell',
  remotes: {
    // Alias → where to load the remote at runtime
    products: 'products@http://localhost:3001/remoteEntry.js',
    // In production: 'products@https://cdn.example.com/products/remoteEntry.js'
  },
  shared: {
    react: { singleton: true, requiredVersion: '^18.0.0' },
    'react-dom': { singleton: true, requiredVersion: '^18.0.0' },
  },
});

// apps/shell/src/App.tsx - consume remote components
import { lazy, Suspense } from 'react';
import { ErrorBoundary } from 'react-error-boundary';

// RemoteProductList is loaded from the products MFE at RUNTIME
const RemoteProductList = lazy(() => import('products/ProductList'));

function App() {
  return (
    <ErrorBoundary fallback={<div>Products MFE unavailable</div>}>
      <Suspense fallback={<ProductListSkeleton />}>
        <RemoteProductList category="electronics" />
      </Suspense>
    </ErrorBoundary>
  );
}

// TypeScript declarations for Module Federation imports
// apps/shell/src/declarations.d.ts
declare module 'products/ProductList' {
  import { ComponentType } from 'react';
  const ProductList: ComponentType<{ category?: string }>;
  export default ProductList;
}

declare module 'products/ProductCard' {
  import { ComponentType } from 'react';
  const ProductCard: ComponentType<{ productId: string }>;
  export default ProductCard;
}

// ---- Vite Module Federation (modern alternative) ----
// npm install @originjs/vite-plugin-federation
import federation from '@originjs/vite-plugin-federation';

// Remote (vite.config.ts)
export default defineConfig({
  plugins: [
    react(),
    federation({
      name: 'products',
      filename: 'remoteEntry.js',
      exposes: { './ProductList': './src/ProductList' },
      shared: ['react', 'react-dom'],
    }),
  ],
  build: { target: 'esnext', minify: false },
});

// Host (vite.config.ts)
export default defineConfig({
  plugins: [
    react(),
    federation({
      remotes: { products: 'http://localhost:3001/assets/remoteEntry.js' },
      shared: ['react', 'react-dom'],
    }),
  ],
});

Micro-frontend Patterns & Tradeoffs

  • When to use MFE: Large teams (10+ developers), where independent deployment velocity justifies the operational complexity overhead
  • When NOT to use MFE: Small teams, early-stage products, monorepos already solve the team autonomy problem with shared deployment
  • Communication patterns: Custom DOM events for cross-MFE events, shared Redux/Zustand store in the shell for truly shared state, URL as shared state (preferred - no coupling)
  • Singleton dependencies: ALL MFEs must declare React as `singleton: true` with matching version; two React instances = broken hooks, impossible to debug
  • Dynamic remotes: Load remote URLs from a config service at runtime → deploy a new MFE version without redeploying the shell → true independent deployment
  • Error boundaries per remote: Each remote module load can fail (network error, deploy failure); wrap every remote import in ErrorBoundary so one MFE failure doesn't crash the shell

Tip

Tip

Practice Microfrontends with Module Federation in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Visual architecture and flow for Micro-frontends with Module Federation

Practice Task

Note

Practice Task - (1) Write a working example of Microfrontends with Module Federation from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with Microfrontends with Module Federation is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react code.

Key Takeaways

  • Micro-frontends split a large frontend into independently deployable pieces - different teams own different sections, can use different frameworks, and deploy independently.
  • When to use MFE: Large teams (10+ developers), where independent deployment velocity justifies the operational complexity overhead
  • When NOT to use MFE: Small teams, early-stage products, monorepos already solve the team autonomy problem with shared deployment
  • Communication patterns: Custom DOM events for cross-MFE events, shared Redux/Zustand store in the shell for truly shared state, URL as shared state (preferred - no coupling)

Hands-on Practice Tools for this Course

Test what you learn using these free in-browser developer utilities (no signup required).

All 40+ Tools

Topics in This Module

0/8 done