React + Module Federation
Micro-frontends: host and remote as separate projects, loaded at runtime. This page shows exactly what pickstack adds to a React project for Module Federation: the packages, the new files and the changes to existing ones. Every combination is installed, built, linted and tested in CI.
$ npm create pickstack@latest my-app -- --framework react --modules module-federation --yesCustomize in the configurator
New files
node_modules dist .env .env.local .DS_Store
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>my-app</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
{
"name": "my-app-host",
"private": true,
"version": "0.1.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc --noEmit && vite build",
"preview": "vite preview",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"react": "^19.3.1",
"react-dom": "^19.3.1"
},
"devDependencies": {
"@module-federation/vite": "^1.25.0",
"@types/react": "^19.3.0",
"@types/react-dom": "^19.3.0",
"@vitejs/plugin-react": "^6.3.0",
"typescript": "~6.0.3",
"vite": "^8.3.8"
}
}
# my-app-host
Stack: React 19, Vite, TypeScript, Module Federation.
## Getting started
```bash
npm install
npm run dev
```
## Scripts
- `npm run build`
- `npm run preview`
- `npm run typecheck`
## Micro-frontends
This is the Module Federation host (port 5173). It loads `Counter` from the remote project at runtime (`http://localhost:5174/remoteEntry.js`), so start the remote first: if it is unreachable, this page stays blank. Before deploying, point the remote `entry` in vite.config at the remote's real URL.
## Push to GitHub
Create an empty repository on GitHub, then:
```bash
git init
git add .
git commit -m "Initial commit"
git branch -M main
git remote add origin https://github.com/<your-username>/my-app-host.git
git push -u origin main
```
import { lazy, Suspense } from "react";
import Counter from "./components/Counter";
const RemoteCounter = lazy(() => import("remote/Counter"));
export default function App() {
return (
<main className="app">
<h1 className="title">my-app</h1>
<Counter />
<Suspense fallback={<p className="text">Loading the remote counter…</p>}>
<RemoteCounter />
</Suspense>
</main>
);
}
import { useState } from "react";
export default function Counter() {
const [count, setCount] = useState(0);
return (
<div className="card">
<p className="count">{count}</p>
<div className="row">
<button
type="button"
className="btn"
onClick={() => setCount((n) => n - 1)}
>
-1
</button>
<button
type="button"
className="btn"
onClick={() => setCount((n) => n + 1)}
>
+1
</button>
</div>
</div>
);
}
:root {
font-family: system-ui, sans-serif;
color: #0f172a;
background: #f8fafc;
}
body {
margin: 0;
}
.app {
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 2rem;
padding: 2rem;
box-sizing: border-box;
}
.title {
font-size: 1.875rem;
margin: 0;
}
.nav {
display: flex;
gap: 1rem;
}
.link {
color: #475569;
text-decoration: none;
font-weight: 500;
}
.link:hover {
color: #0f172a;
}
.page {
display: flex;
flex-direction: column;
align-items: center;
gap: 1.5rem;
}
.text {
max-width: 28rem;
text-align: center;
color: #475569;
}
.card {
background: #fff;
padding: 2rem;
border-radius: 1rem;
box-shadow: 0 1px 3px rgba(15, 23, 42, 0.08);
text-align: center;
}
.count {
font-size: 3rem;
font-weight: 700;
margin: 0;
font-variant-numeric: tabular-nums;
}
.row {
margin-top: 1.5rem;
display: flex;
justify-content: center;
gap: 0.75rem;
}
.btn {
background: #0f172a;
color: #fff;
border: 0;
border-radius: 0.5rem;
padding: 0.5rem 1rem;
font: inherit;
cursor: pointer;
}
.btn:hover {
background: #334155;
}
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import App from "./App";
import "./index.css";
createRoot(document.getElementById("root")!).render(
<StrictMode>
<App />
</StrictMode>,
);
declare module "remote/Counter" {
import type { ComponentType } from "react";
const Counter: ComponentType;
export default Counter;
}
{
"compilerOptions": {
"target": "ES2022",
"lib": ["ES2022", "DOM", "DOM.Iterable"],
"module": "ESNext",
"moduleResolution": "bundler",
"jsx": "react-jsx",
"strict": true,
"noEmit": true,
"skipLibCheck": true,
"isolatedModules": true,
"verbatimModuleSyntax": true,
"types": ["vite/client"]
},
"include": ["src", "vite.config.ts"]
}
import { federation } from "@module-federation/vite";
import react from "@vitejs/plugin-react";
import { defineConfig } from "vite";
export default defineConfig({
plugins: [
react(),
federation({
name: "host",
dts: false,
remotes: {
remote: {
type: "module",
name: "remote",
entry: "http://localhost:5174/remoteEntry.js",
},
},
shared: {
react: { singleton: true },
"react-dom": { singleton: true },
},
}),
],
server: { port: 5173, strictPort: true },
preview: { port: 5173, strictPort: true },
});
node_modules dist
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>remote</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
{
"name": "my-app-remote",
"private": true,
"version": "0.1.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc --noEmit && vite build",
"preview": "vite preview",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"react": "^19.3.1",
"react-dom": "^19.3.1"
},
"devDependencies": {
"@module-federation/vite": "^1.25.0",
"@types/react": "^19.3.0",
"@types/react-dom": "^19.3.0",
"@vitejs/plugin-react": "^6.3.0",
"typescript": "~6.0.3",
"vite": "^8.3.8"
}
}
# my-app-remote
Module Federation remote. It exposes `./Counter` at `http://localhost:5174/remoteEntry.js`, which the host loads at runtime.
```bash
npm install
npm run dev
```
Before deploying, set `REMOTE_ORIGIN` in vite.config to the URL the remote is served from, and serve it with CORS headers so the host's origin can load it.
import { useState } from "react";
export default function Counter() {
const [count, setCount] = useState(0);
return (
<div className="card">
<p className="count">{count}</p>
<div className="row">
<button
type="button"
className="btn"
onClick={() => setCount((n) => n - 1)}
>
-1
</button>
<button
type="button"
className="btn"
onClick={() => setCount((n) => n + 1)}
>
+1
</button>
</div>
</div>
);
}
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import Counter from "./Counter";
// Standalone page for working on the remote without the host.
createRoot(document.getElementById("root")!).render(
<StrictMode>
<Counter />
</StrictMode>,
);
{
"compilerOptions": {
"target": "ES2022",
"lib": ["ES2022", "DOM", "DOM.Iterable"],
"module": "ESNext",
"moduleResolution": "bundler",
"jsx": "react-jsx",
"strict": true,
"noEmit": true,
"skipLibCheck": true,
"isolatedModules": true,
"verbatimModuleSyntax": true,
"types": ["vite/client"]
},
"include": ["src", "vite.config.ts"]
}
import { federation } from "@module-federation/vite";
import react from "@vitejs/plugin-react";
import { defineConfig } from "vite";
// Where this remote is served. Asset URLs resolve against it, so they keep working
// when the host loads the remote; change it (and the host's remote entry) on deploy.
const REMOTE_ORIGIN = "http://localhost:5174";
export default defineConfig({
base: `${REMOTE_ORIGIN}/`,
server: { port: 5174, strictPort: true, origin: REMOTE_ORIGIN },
preview: { port: 5174, strictPort: true },
plugins: [
react(),
federation({
name: "remote",
filename: "remoteEntry.js",
manifest: true,
dts: false,
exposes: {
"./Counter": "./src/Counter.tsx",
},
shared: {
react: { singleton: true },
"react-dom": { singleton: true },
},
}),
],
});
Project files
README.mdhost/.gitignorehost/README.mdhost/index.htmlhost/package.jsonhost/src/App.tsxhost/src/components/Counter.tsxhost/src/index.csshost/src/main.tsxhost/src/remotes.d.tshost/tsconfig.jsonhost/vite.config.tsremote/.gitignoreremote/README.mdremote/index.htmlremote/package.jsonremote/src/Counter.tsxremote/src/main.tsxremote/tsconfig.jsonremote/vite.config.ts