In the concept of Module Federation a remote is the term given to an application that exposes modules that can be shared to and consumed by host applications.
This is the key difference between a remote and host.
- A
remoteexposes modules that can be consumed - A
hostconsumes exposed modules
Nx includes first-class support for helping you to scaffold a Module Federation Architecture for your React and Angular application(s).
Generating a Remote
Section titled “Generating a Remote”To generate a remote application in your workspace, cd into the right folder and run the following command:
NX Generating @nx/react:remote
CREATE apps/react/myremote/src/app/app.spec.tsxCREATE apps/react/myremote/src/assets/.gitkeepCREATE apps/react/myremote/src/environments/environment.prod.tsCREATE apps/react/myremote/src/environments/environment.tsCREATE apps/react/myremote/src/favicon.icoCREATE apps/react/myremote/src/index.htmlCREATE apps/react/myremote/tsconfig.app.jsonCREATE apps/react/myremote/rspack.config.tsCREATE apps/react/myremote/.babelrcCREATE apps/react/myremote/src/app/nx-welcome.tsxCREATE apps/react/myremote/src/app/app.module.cssCREATE apps/react/myremote/src/app/app.tsxCREATE apps/react/myremote/src/styles.cssCREATE apps/react/myremote/tsconfig.jsonCREATE apps/react/myremote/project.jsonCREATE apps/react/myremote/.eslintrc.jsonCREATE apps/react/myremote/jest.config.tsCREATE apps/react/myremote/tsconfig.spec.jsonCREATE apps/react/myremote/src/bootstrap.tsxCREATE apps/react/myremote/module-federation.config.tsCREATE apps/react/myremote/src/main.tsCREATE apps/react/myremote/src/remote-entry.tsUPDATE tsconfig.base.jsonNX Generating @nx/angular:host
CREATE apps/angular/myremote/project.jsonCREATE apps/angular/myremote/src/assets/.gitkeepCREATE apps/angular/myremote/src/favicon.icoCREATE apps/angular/myremote/src/index.htmlCREATE apps/angular/myremote/src/styles.cssCREATE apps/angular/myremote/tsconfig.app.jsonCREATE apps/angular/myremote/tsconfig.jsonCREATE apps/angular/myremote/src/app/app.tsCREATE apps/angular/myremote/src/app/app.routes.tsCREATE apps/angular/myremote/src/main.tsCREATE apps/angular/myremote/.eslintrc.jsonCREATE apps/angular/myremote/jest.config.tsCREATE apps/angular/myremote/src/test-setup.tsCREATE apps/angular/myremote/tsconfig.spec.jsonCREATE apps/angular/myremote/src/app/remote-entry/entry.tsCREATE apps/angular/myremote/src/app/remote-entry/entry.routes.tsCREATE apps/angular/myremote/src/app/remote-entry/nx-welcome.tsCREATE apps/angular/myremote/module-federation.config.tsCREATE apps/angular/myremote/webpack.config.tsCREATE apps/angular/myremote/webpack.prod.config.tsCREATE apps/angular/myremote/src/bootstrap.tsUPDATE tsconfig.base.jsonAfter the remote application is generated, you can then update your host application's Module Federation Config File to specify that it can consume federated modules from this remote.
import { ModuleFederationConfig } from '@nx/module-federation';
const config: ModuleFederationConfig = { name: 'shell', remotes: ['myremote'], // <-- add the name of your remote to the remotes array};export default config;module.exports = { name: 'shell', remotes: ['myremote'], // <-- add the name of your remote to the remotes array};Let Nx add your Remote to your Host
Section titled “Let Nx add your Remote to your Host”The remote generators also allow you to specify a --host option, which will allow the generator to add your remote to your host automatically. This can save you time by skipping the manual step above.
The command would look like the following:
NX Generating @nx/react:remote
CREATE apps/react/myremote/src/app/app.spec.tsxCREATE apps/react/myremote/src/assets/.gitkeepCREATE apps/react/myremote/src/environments/environment.prod.tsCREATE apps/react/myremote/src/environments/environment.tsCREATE apps/react/myremote/src/favicon.icoCREATE apps/react/myremote/src/index.htmlCREATE apps/react/myremote/tsconfig.app.jsonCREATE apps/react/myremote/rspack.config.tsCREATE apps/react/myremote/.babelrcCREATE apps/react/myremote/src/app/nx-welcome.tsxCREATE apps/react/myremote/src/app/app.module.cssCREATE apps/react/myremote/src/app/app.tsxCREATE apps/react/myremote/src/styles.cssCREATE apps/react/myremote/tsconfig.jsonCREATE apps/react/myremote/project.jsonCREATE apps/react/myremote/.eslintrc.jsonCREATE apps/react/myremote/jest.config.tsCREATE apps/react/myremote/tsconfig.spec.jsonCREATE apps/react/myremote/src/bootstrap.tsxCREATE apps/react/myremote/module-federation.config.tsCREATE apps/react/myremote/src/main.tsCREATE apps/react/myremote/src/remote-entry.tsUPDATE apps/react/shell/module-federation.config.tsUPDATE tsconfig.base.json> NX Generating @nx/angular:host
CREATE apps/angular/myremote/project.jsonCREATE apps/angular/myremote/src/assets/.gitkeepCREATE apps/angular/myremote/src/favicon.icoCREATE apps/angular/myremote/src/index.htmlCREATE apps/angular/myremote/src/styles.cssCREATE apps/angular/myremote/tsconfig.app.jsonCREATE apps/angular/myremote/tsconfig.jsonCREATE apps/angular/myremote/src/app/app.tsCREATE apps/angular/myremote/src/app/app.routes.tsCREATE apps/angular/myremote/src/main.tsCREATE apps/angular/myremote/.eslintrc.jsonCREATE apps/angular/myremote/jest.config.tsCREATE apps/angular/myremote/src/test-setup.tsCREATE apps/angular/myremote/tsconfig.spec.jsonCREATE apps/angular/myremote/src/app/remote-entry/entry.tsCREATE apps/angular/myremote/src/app/remote-entry/entry.routes.tsCREATE apps/angular/myremote/src/app/remote-entry/nx-welcome.tsCREATE apps/angular/myremote/module-federation.config.tsCREATE apps/angular/myremote/webpack.config.tsCREATE apps/angular/myremote/webpack.prod.config.tsCREATE apps/angular/myremote/src/bootstrap.tsUPDATE apps/angular/shell/module-federation.config.tsUPDATE tsconfig.base.jsonBuilding your Remote
Section titled “Building your Remote”Your remote application acts like any other application in the context of Nx, and therefore building it as simple as running:
nx build myremoteServing your Remote
Section titled “Serving your Remote”The remote application is generated with two serve-like targets. These are:
- serve
- serve-static
They can be run as usual with Nx:
nx serve myremotenx serve-static myremoteServe Target
Section titled “Serve Target”The serve target will use webpack-dev-server to serve your application, allowing for HMR and Live Reload. This is useful when you're working locally on that specific remote application.
Serve-Static Target
Section titled “Serve-Static Target”The serve-static target will first build your application, storing the build artifact in the defined output directory (usually dist/path/to/remote). It will then use http-server to serve the built application locally.
This is less memory and CPU intensive than webpack-dev-server but it does not support HMR or Live Reload.
The purpose of the serve-static target is to allow you to serve your host application, along with all of the remote applications it depends on without being too resource intensive.
This has been further expanded upon. When you serve the host application, Nx will build (or pull from cache) your remote applications and serve them all via a single file server, to further reduce resource consumption.
Serving your Remote via your Host
Section titled “Serving your Remote via your Host”Generally, your host is the main application that you deploy and that users visit. It consumes modules from remote applications, but those remote applications are usually never visited directly by a user.
Therefore, to support developing your application in a manner that replicates how users use the application, when serving a host application, Nx will serve all the dependent remotes automatically.
By default, the dependent remote applications will be served via the serve-static command. However, if you are working on a specific remote application, you can tell Nx to serve the host application and any number of remote applications via the webpack-dev-server allowing those remote applications to take advantage of HMR and Live Reloading as you work on them.
To do this, run the command:
## Reactnx serve myremote
## AngularThis informs Nx to run the serve command of myremote, rather than the serve-static command.