Build a Components Remote with Vite
Expose client components from a Vite project with nextFederationComponents.
Build a remote that exposes client components with nextFederationComponents. To render them, see Host configuration.
Start from a create-vite project
npm create vite@latest widgets -- --template react-ts scaffolds more than a remote needs. Make these changes:
@vitejs/plugin-react: remove it from the remote's config. An exposed module compiled with Fast Refresh fails the build (NF-002), because a host page has no Vite preamble.index.htmlandsrc/main.tsx: the build reads only the files inexposes. Delete the scaffold page, or keep it as a standalone app. Exposed modules must not import from it.- Scripts: use
"build": "vite build"and"start": "nf-serve --dir dist --port 4110".vite previewdoes not serve a remote. - Dependencies: add
@next-federation/viteand@next-federation/serveras dev dependencies. Pinreactandreact-domto one exact version. next: add it as a dev dependency, for types, if an exposed module importsnext/linkornext/navigation(Links).- TypeScript:
types: trueneeds TypeScript 5 or 6. TypeScript 7 ships no compiler API: the build warnsNF-002and publishes no types. Pintypescript@~6in a remote that publishes types.
Expose a component and build it
List each component in the plugin config.
import { nextFederationComponents } from '@next-federation/vite'
import { defineConfig } from 'vite'
export default defineConfig({
plugins: [
nextFederationComponents({
name: 'shop', // the Module Federation container name (a JS identifier)
base: 'https://shop.example.com/',
exposes: { './Cart': './src/Cart.tsx' },
// Modules shared with the host, besides React (see Sharing modules):
shared: ['@acme/host-context'],
}),
],
})vite build writes dist/<buildId>/. The build id is buildId, NF_BUILD_ID or the build time. Serve it with nf-serve:
/_nf/components.jsonis the current build's descriptor (Cache-Control: no-store)./b/<buildId>/…serves every build kept by retention.- A retired build answers
410(NF-023).
The plugin pins @module-federation/vite to exactly 1.24.0 (NF-026 otherwise). React is always host-provided. Build output lists every file.
Keep a standalone app
The plugin applies to the whole Vite project: React and every shared module become host-provided, so a page of the project fails in its own dev server (createContext is not a function). To keep the app running, give the remote its own config.
import { nextFederationComponents } from '@next-federation/vite'
import { defineConfig } from 'vite'
export default defineConfig({
plugins: [
nextFederationComponents({
name: 'widgets',
base: 'https://widgets.example.com/',
outDir: 'dist-remote',
exposes: { './Counter': './src/components/Counter.tsx' },
}),
],
}){
"scripts": {
"dev": "vite",
"build": "vite build",
"build:remote": "vite build --config vite.remote.config.ts",
"start:remote": "nf-serve --dir dist-remote --port 4110"
}
}Keep create-vite's vite.config.ts (with @vitejs/plugin-react) for the app. Use a separate outDir, so neither build empties the other's folder.
Accept React 18
The default range is React 19 only. To accept React 18 hosts too, set react.
nextFederationComponents({
name: 'shop',
base: 'https://shop.example.com/',
exposes: { './Cart': './src/Cart.tsx' },
react: '^18.3.0 || ^19.0.0',
})The range must be valid semver (NF-002 otherwise). See React versions for the upgrade order and APIs to avoid.
Generate a signing key
Good to know: A build without the key is unsigned, and nothing stops it.
vite build and nf-describe write an unsigned descriptor without failing.
Hosts then render the build in the browser only (NF-028).
Set the key in every build and describe step, then check the result with nf-describe --verify --public-key.
node -e "
const { generateKeyPairSync } = require('node:crypto')
const { privateKey } = generateKeyPairSync('ec', { namedCurve: 'P-256' })
const jwk = { ...privateKey.export({ format: 'jwk' }), kid: 'shop-2026-11' }
const { d, ...pub } = jwk
console.log('NF_COMPONENTS_SIGNING_KEY=' + JSON.stringify(jwk))
console.log('public key for the host: ' + JSON.stringify(pub))
"The kid is a label that tells keys apart when you rotate. Keep the private key (NF_COMPONENTS_SIGNING_KEY) in the remote's build environment, as a CI secret. Put the public key in the host's components.ssr.publicKeys (Host configuration). To rotate or pin, see Integrity.