Quick Start Guide
This guide will lead you step by step to learn how to use Module Federation. We will build two independent Single Page Applications (SPAs) that will share components using Module Federation. If you encounter unfamiliar terms in the following text, please refer to the Glossary.
Requirements
When using Module Federation build plugins, make sure your Node.js version is 20 or higher.
If you are adding Module Federation to an existing project, start with Integrations and choose the package for your framework or bundler.
Creating a New Project
Module Federation provides the create-module-federation tool to create projects. You don't need to install it globally; you can directly use npx to run it on-demand.
You can use create-module-federation to create a Module Federation project by running the following command:
Just follow the prompts step by step. During the creation process, you can select the project type, role type, etc.
Templates
When creating a project, you can choose from the following templates provided by create-module-federation:
Quick Creation
create-module-federation provides some CLI options. By setting these CLI options, you can skip the interactive selection steps and create a project with one click.
For example, to create a Modern.js provider project named provider in the my-project directory with one click:
The complete CLI options for create-module-federation are as follows:
Create a provider
Execute the create-module-federation command, select the required framework and type according to your needs, and select the role type as provider to create a project.
Here we take creating an rsbuild provider project as an example:
Create a consumer
Execute the create-module-federation command, select the required framework and type according to your needs, and select the role type as consumer to create a project.
Here is an example of creating an rsbuild consumer project:
Replace provider
The default consumer project created will consume a published provider. If you want to replace the provider, you need to modify the remotes configuration in module-federation.config.ts.
Existing project integration
If you want to integrate Module Federation into an existing project, start with Integrations. It explains which package to install for Rsbuild, Rspack, Webpack, Modern.js, Next.js, Vite, and other project types.
Summary
Through the above process, you have completed the export of a component from a 'provider' for use by a 'consumer' based on Module Federation. Along the way, you have preliminarily used and understood the configurations of remotes, exposes, and shared in the Module Federation plugin.
If you want to add Module Federation to an existing project, start from Integrations and choose the matching path.
Next Steps
You may want to: