|
| 1 | +# {props.name || 'Module Federation'} on Node.js, Made Easy |
| 2 | + |
| 3 | +import RuntimeConsumerDemo from '@components/common/node/runtime-consumer-demo'; |
| 4 | +import HostRspackConfig from '@components/common/node/host-rspack-config'; |
| 5 | +import HostUsage from '@components/common/node/host-usage'; |
| 6 | +import RemoteRslibConfig from '@components/common/node/remote-rslib-config'; |
| 7 | +import RemoteRspackConfig from '@components/common/node/remote-rspack-config'; |
| 8 | + |
| 9 | +{props.name || 'Module Federation'} supports Node.js out of the box. Whether you are consuming modules at runtime only, or integrating into a Webpack/Rspack build pipeline, it can be adopted with a relatively small amount of configuration. This document walks through common ways to use {props.name || 'Module Federation'} in Node.js. |
| 10 | + |
| 11 | +## Overview |
| 12 | + |
| 13 | +In a Node.js server application, you can load remote modules via {props.name || 'Module Federation'}. These modules can be local files built in CommonJS format, or remote services accessed over HTTP. This enables a flexible foundation for server-side microservices, dynamic feature delivery, and shared resources. |
| 14 | + |
| 15 | +## Consumer |
| 16 | + |
| 17 | +### Runtime-Only |
| 18 | + |
| 19 | +If you are only consuming modules in Node.js and do not want to introduce bundlers like Webpack/Rspack, you can use the runtime-only approach. The key idea is: **no build plugins required**. You only need the APIs provided by { props.runtime || '@module-federation/runtime' }. |
| 20 | + |
| 21 | +Steps: |
| 22 | + |
| 23 | +1. **Create an MF instance** with `createInstance`. |
| 24 | +2. **Register remotes** via the `remotes` array. |
| 25 | +3. **Load modules** via `loadRemote`. |
| 26 | + |
| 27 | +The following example shows how to load a remote module exposed over HTTP: |
| 28 | + |
| 29 | +{props.runtimeConsumerDemo || <RuntimeConsumerDemo />} |
| 30 | + |
| 31 | +### Using a Bundler Plugin (Rspack/Webpack) |
| 32 | + |
| 33 | +If your Node.js application is built with Webpack or Rspack, integrating {props.name || 'Module Federation'} is straightforward: add the plugin and the required runtime configuration. |
| 34 | + |
| 35 | +For the Host (consumer), the key is to add `@module-federation/node/runtimePlugin` and set `remoteType: 'script'` and `target: 'async-node'`, then apply the rest of the configuration. |
| 36 | + |
| 37 | +Rspack example (Webpack is largely the same): |
| 38 | + |
| 39 | +{props.hostRspackConfig || <HostRspackConfig />} |
| 40 | + |
| 41 | +After that, you can directly `import` remote modules in your code: |
| 42 | + |
| 43 | +{props.hostUsage || <HostUsage />} |
| 44 | + |
| 45 | +## Provider |
| 46 | + |
| 47 | +On the producer side, we recommend using Rslib. You only need to use the { props.rsbuildPlugin || '@module-federation/rsbuild-plugin' } plugin and set `target: 'async-node'` to generate a remote that can be consumed in Node.js. |
| 48 | + |
| 49 | +Key configuration from `apps/node-remote/rslib.config.ts`: |
| 50 | + |
| 51 | +{props.remoteRslibConfig || <RemoteRslibConfig />} |
| 52 | + |
| 53 | +If you are not using Rslib, you can also build the remote with Rspack/Webpack (the configuration is largely the same). The key points are: set `target` to `async-node`, and output `remoteEntry.js` with `library.type = 'commonjs-module'`. |
| 54 | + |
| 55 | +Rspack example: |
| 56 | + |
| 57 | +{props.remoteRspackConfig || <RemoteRspackConfig />} |
| 58 | + |
| 59 | +Webpack configuration is largely the same as the Rspack example above. |
| 60 | + |
| 61 | +## FAQ |
| 62 | + |
| 63 | +### 1. What does `target: 'async-node'` do? |
| 64 | + |
| 65 | +`target: 'async-node'` is a Webpack build target that produces output suitable for Node.js with asynchronous loading. This is important for Module Federation’s dynamic, async loading model—especially when you need top-level `await` while loading remotes. |
| 66 | + |
| 67 | +### 2. Why do I need to set `remoteType: 'script'`? |
| 68 | + |
| 69 | +Currently, the MF bundler runtime only supports the `script` remote loading type, so for Node.js consumption you need to explicitly set `remoteType: 'script'`. |
| 70 | + |
| 71 | +## References |
| 72 | + |
| 73 | +- **Host (Consumer) example config**: [`apps/node-host/webpack.config.js`](https://github.com/module-federation/core/blob/main/apps/node-host/webpack.config.js) |
| 74 | +- **Host (Consumer) example code**: [`apps/node-host/src/main.js`](https://github.com/module-federation/core/blob/main/apps/node-host/src/main.js) |
| 75 | +- **Remote (Producer) Rslib example config**: [`apps/node-remote/rslib.config.ts`](https://github.com/module-federation/core/blob/main/apps/node-remote/rslib.config.ts) |
| 76 | +- **Remote (Producer) Webpack example config**: [`apps/node-remote/webpack.config.js`](https://github.com/module-federation/core/blob/main/apps/node-remote/webpack.config.js) |
0 commit comments