A boilerplate setup for Module Federation using Webpack, designed for building scalable micro-frontend architectures. This kit provides a complete setup for both host and remote applications to share React components dynamically.
- 🎯 Webpack 5 with Module Federation: Seamless sharing of code across independent apps.
- ⚛️ React + TypeScript: Fully typed setup with React best practices.
- 🔥 Hot Module Replacement: Built-in React Fast Refresh for blazing-fast development.
- 🧩 Multiple Environments: Easily configure development, staging, and production.
- 🛡️ Secured Remote Access: Protects remote modules with custom Express middleware.
- 📦 Optimized Builds: Production-ready with tree shaking, minification, and code splitting.
Make sure you have the following installed:
- Node.js (v20 or higher)
- Clone the repo
git clone https://github.com/your-username/module-federation-webpack-kit.git
cd module-federation-webpack-kit
- Install dependencies
npm install
# or
pnpm install
This project includes a host and a remote application with pre-configured Module Federation settings. You can run both to see them working together!
npm run start:host
# or
pnpm start:host
🌐 Runs on http://localhost:3000
npm run start:remote
# or
pnpm start:remote
🌐 Runs on http://localhost:3001
The remote app is served using an Express server that includes customizable middleware for security. This ensures only authorized access to your exposed modules.
🔒 Middleware can be configured to:
- ✅ Allowlist specific hosts
- 🪪 Require auth tokens
- 🌍 Restrict by environment or IP
- 🛑 Block unwanted requests before serving federated modules
Example logic is in server.js
. Modify it to suit your team’s needs!
Shared config used by both host and remote.
Development-only config:
- HMR
- Fast Refresh
- Source maps
Production config:
- Minification
- Tree shaking
- Module federation
Remote App
new ModuleFederationPlugin({
name: "remoteApp",
filename: "remoteEntry.js",
exposes: {
"./RemoteComponent": "./src/components/RemoteComponent",
},
shared: {
react: { singleton: true, eager: true, requiredVersion: deps.react },
"react-dom": {
singleton: true,
eager: true,
requiredVersion: deps["react-dom"],
},
},
});
Host App
const RemoteComponent = React.lazy(() => import("remoteApp/RemoteComponent"));
Use it like this:
<Suspense fallback={<div>Loading Remote Component...</div>}>
<RemoteComponent />
</Suspense>
npm run build:host
# or
pnpm build:host
npm run build:remote
# or
pnpm build:remote
Outputs go to the
dist/
directory.
You can build apps for multiple environments (e.g. development
, staging
, production
) using custom NODE_ENV
values.
Example for staging:
"build:staging": "NODE_ENV=staging webpack --config webpack.config.js --mode production"
Staging runs in production mode to reflect real behavior but uses separate configs or env variables.
- Remote exposes components with
ModuleFederationPlugin
. - Host dynamically imports these components with
React.lazy
. - At runtime, Webpack loads them from the remote server!
This makes each app independently deployable while still being integrated.
- ✅ Start host and remote apps.
- 🛠️ Add new components in the remote and expose them.
- 🧲 Lazy load new components in the host.
- 🔒 Use middleware to secure remote in non-dev environments.
- 🚀 Deploy independently!
If the remote app fails to load, avoid crashing the app:
<Suspense fallback={<div>Loading Remote Component...</div>}>
<ErrorBoundary fallback={<div>Failed to load remote component.</div>}>
<RemoteComponent />
</ErrorBoundary>
</Suspense>
Wrap lazy imports in an error boundary for safety 🚧
Feel free to open issues, submit PRs, or just ⭐️ the repo if you like it!