<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ module federation - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ module federation - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Fri, 26 Jun 2026 22:47:20 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/module-federation/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How to Build Micro Frontends in React with Vite and Module Federation ]]>
                </title>
                <description>
                    <![CDATA[ Micro Frontend Architecture has become increasingly popular in recent years, as teams look to re-use parts of their existing applications in new projects rather than rebuilding everything from scratch. Micro frontends also allow large teams to share ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-build-micro-frontends-in-react-with-vite-and-module-federation/</link>
                <guid isPermaLink="false">68ae1d943a7c9745e83e7797</guid>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ module federation ]]>
                    </category>
                
                    <category>
                        <![CDATA[ vite ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Microfrontend ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Grant Riordan ]]>
                </dc:creator>
                <pubDate>Tue, 26 Aug 2025 20:48:20 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1756231755011/283b9e67-9a09-4241-9d90-701cb075084d.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Micro Frontend Architecture has become increasingly popular in recent years, as teams look to re-use parts of their existing applications in new projects rather than rebuilding everything from scratch.</p>
<p>Micro frontends also allow large teams to share common components – such as headers, footers, and logins – across multiple applications, ensuring consistency and keeping their projects DRY (Don’t Repeat Yourself).</p>
<p>In this article, you will learn:</p>
<ul>
<li><p>How to set up a project and folder structure that implements a Micro Frontend (MFE) Infrastructure</p>
</li>
<li><p>How to use and configure the <code>@originjs/vite-plugin-federation</code> to allow Module Federation (MF) usage with Vite projects.</p>
</li>
<li><p>How to share and consume React components between apps.</p>
</li>
<li><p>How to run and test your app with shared components locally</p>
</li>
</ul>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ul>
<li><p><a class="post-section-overview" href="#heading-pre-requisites">Pre-Requisites</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-what-is-module-federation">What Is Module Federation?</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-benefits-of-module-federation">Benefits of Module Federation</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-project-structure">Project Structure</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-create-the-projects">How to Create the Projects</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-install-dependencies">How to Install Dependencies</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-configure-the-remote-app">How to Configure the Remote App</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-the-vite-module-federation-plugin">The Vite Module Federation Plugin</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-key-constraints-on-exported-modules">Key Constraints On Exported Modules</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-create-the-remote-components">How to Create the Remote Components</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-consume-the-remote-components-within-the-host">How to Consume the Remote Components Within the Host</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-handle-typescript-errors">How to Handle TypeScript Errors</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-add-remotewrappercomponent-to-apptsx">How to Add RemoteWrapperComponent to App.tsx</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-serve-the-remote-app-and-run-your-host">How to Serve the Remote App and Run Your Host</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-the-true-power-of-micro-frontends">The True Power of Micro Frontends</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-final-thoughts">Final Thoughts</a></p>
</li>
</ul>
<h2 id="heading-pre-requisites">Pre-Requisites</h2>
<ul>
<li><p>Node installed on your machine – you can get your download of Node <a target="_blank" href="https://nodejs.org/en/download">here</a></p>
</li>
<li><p>Familiarity with JS / TS and React</p>
</li>
<li><p>Familiarity with Command Line / Terminal</p>
</li>
</ul>
<h2 id="heading-what-is-module-federation">What Is Module Federation?</h2>
<p>Before we go any further, let’s talk about Module Federation (MF).</p>
<p>Module Federation is a technique in web development that allows multiple separate builds to work together as a single application. It enables the sharing of code between different, independent applications at runtime, rather than at build time. This means a host application can dynamically load and execute code from a remote application.</p>
<p>At its core, Module Federation uses a <strong>"host"</strong> and <strong>"remote"</strong> architecture. A <strong>host</strong> application is the main app that consumes shared code / components. A <strong>remote</strong> application is the one that exposes code to be consumed.</p>
<p>The remote app specifies <strong>which</strong> parts of its code, or "modules," are available for others to use. The host then references these modules and loads them as needed.</p>
<h2 id="heading-benefits-of-module-federation">Benefits of Module Federation</h2>
<h3 id="heading-independent-deployment">Independent Deployment</h3>
<p>Module Federation lets teams build and deploy their micro-frontends separately. This eliminates the need for full application redeployments, accelerating development and reducing risk.</p>
<h3 id="heading-efficient-code-sharing">Efficient Code Sharing</h3>
<p>MF provides a native way to share code and dependencies between micro-frontends. This prevents code duplication, reducing bloat and ensuring consistency.</p>
<h3 id="heading-performance-gains">Performance Gains</h3>
<p>By sharing dependencies, Module Federation reduces the overall bundle size and improves initial load times, as each micro-frontend doesn't download its own copy of common libraries.</p>
<h3 id="heading-scalability-and-maintainability">Scalability and Maintainability</h3>
<p>MF enables a scalable architecture by breaking down large applications into smaller, manageable micro-frontends. This makes the codebase easier to maintain and allows teams to work independently.</p>
<h3 id="heading-analogy">Analogy</h3>
<p>Imagine building an online store. Traditionally, you’d create the entire site – homepage, product pages, cart, user profile – as one big application. A small cart change would require rebuilding and redeploying everything.</p>
<p>With Micro Frontends and Module Federation, the shopping cart can be its own app, built and maintained by a dedicated team. The main site simply imports it, allowing the cart team to release updates independently, speeding up development and improving focus.</p>
<p>This also works for organisations with multiple sites that need a consistent look. Shared components like a header, footer, or product card can be reused across sites with different purposes, such as hiring vehicles or selling furniture, ensuring visual consistency while keeping functionality unique.</p>
<h2 id="heading-project-structure">Project Structure</h2>
<p>You will need to create two projects:</p>
<ul>
<li><p><strong>host</strong> – this will act as your host application</p>
</li>
<li><p><strong>remote</strong> – this will expose the components you want to share</p>
</li>
</ul>
<h2 id="heading-how-to-create-the-projects">How to Create the Projects</h2>
<p>Run the following commands in your terminal to create your root project folder and your two Vite projects:</p>
<pre><code class="lang-bash"><span class="hljs-comment"># create micro-frontends directory for two vite projects, and navigate to folder</span>
mkdir micro-frontends; <span class="hljs-built_in">cd</span> micro-frontends
</code></pre>
<h3 id="heading-create-a-git-repository-optional">Create a Git Repository (Optional)</h3>
<p>Using the below command you can create a Git repository for source control.</p>
<pre><code class="lang-bash"><span class="hljs-comment"># initiate git repo</span>
git init
</code></pre>
<pre><code class="lang-bash"><span class="hljs-comment"># create host application vite app</span>
npm create vite@latest host-app

<span class="hljs-comment"># once submitted the command, select React and press Enter, </span>
Select a framework:
│  ○ Vanilla
│  ○ Vue
│  ● React
│  ○ Preact
│  ○ Lit
│  ○ Svelte
│  ○ Solid
│  ○ Qwik
│  ○ Angular
│  ○ Marko
│  ○ Others

<span class="hljs-comment"># select Typescript + SWC and again press Enter</span>
Select a variant:
│  ○ TypeScript
│  ● TypeScript + SWC
│  ○ JavaScript
│  ○ JavaScript + SWC
│  ○ React Router v7 
│  ○ TanStack Router
│  ○ RedwoodSDK 
│  ○ RSC
</code></pre>
<p>Once this is done, navigate back to the root project folder (<code>micro-frontends</code>):</p>
<pre><code class="lang-bash"><span class="hljs-comment"># navigate back</span>
<span class="hljs-built_in">cd</span> ../

<span class="hljs-comment"># create remote-app </span>
npm create vite@latest remote-app

<span class="hljs-comment"># follow instructions as before to select React, Typescript + SWC</span>
</code></pre>
<p>You now have your two projects, <code>host-app</code> and <code>remote-app</code>.</p>
<h2 id="heading-how-to-install-dependencies">How to Install Dependencies</h2>
<p>Open the <code>micro-frontends</code> folder in your preferred IDE / Code Editor. In this tutorial I’ll be using VS Code.</p>
<p>Tip: You can open the current folder in VS Code via your terminal using the following command <code>code .</code> if you’ve already added <code>code</code> to you PATH.</p>
<p>Once you’ve opened VS Code, open the terminal and run the following command:</p>
<pre><code class="lang-bash"><span class="hljs-built_in">cd</span> host-app &amp;&amp; npm install -D @originjs/vite-plugin-federation
</code></pre>
<p>and then run:</p>
<pre><code class="lang-bash"><span class="hljs-built_in">cd</span> ../remote-app &amp;&amp; npm install -D @originjs/vite-plugin-federation
</code></pre>
<h3 id="heading-styling">Styling</h3>
<p>To see styling like my examples, you need to add Tailwind CSS to both your remote and host applications. You can find instructions on how to do so <a target="_blank" href="https://tailwindcss.com/docs/installation/using-vite">here</a></p>
<h2 id="heading-how-to-configure-the-remote-app">How to Configure the Remote App</h2>
<p>In order to be able to utilise modules and components from your remote applications, you need to configure your application to expose those modules, and your host app to consume them.</p>
<p>Use the following configurations in your apps to allow your components to be exposed and consumed – don’t worry about the components just yet, you’ll create them soon.</p>
<h3 id="heading-host-app-viteconfigts">Host App – <code>Vite.config.ts</code></h3>
<p>In the <code>host</code> app, open <code>vite.config.js</code> and add the following configuration:</p>
<pre><code class="lang-typescript"><span class="hljs-comment">//host - vote.config.js</span>
<span class="hljs-keyword">import</span> { defineConfig } <span class="hljs-keyword">from</span> <span class="hljs-string">"vite"</span>;
<span class="hljs-keyword">import</span> react <span class="hljs-keyword">from</span> <span class="hljs-string">"@vitejs/plugin-react-swc"</span>;
<span class="hljs-keyword">import</span> federation <span class="hljs-keyword">from</span> <span class="hljs-string">"@originjs/vite-plugin-federation"</span>;


<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> defineConfig({
  plugins: [
    react(),
    federation({
      name: <span class="hljs-string">"host_app"</span>,
      remotes: {
        remote_app: <span class="hljs-string">"http://localhost:5001/assets/remoteEntry.js"</span>,
      },
      shared: [<span class="hljs-string">"react"</span>, <span class="hljs-string">"react-dom"</span>],
    }),
  ],
  build: {
    modulePreload: <span class="hljs-literal">false</span>,
    target: <span class="hljs-string">"esnext"</span>,
    minify: <span class="hljs-literal">false</span>,
    cssCodeSplit: <span class="hljs-literal">false</span>,
  },
});
</code></pre>
<h3 id="heading-remote-app-viteconfigts">Remote App – <code>Vite.config.ts</code></h3>
<p>In the <code>remote</code> app, open <code>vite.config.js</code> and add the following configuration:</p>
<pre><code class="lang-typescript"><span class="hljs-comment">// remote - vite.config.js</span>
<span class="hljs-keyword">import</span> { defineConfig } <span class="hljs-keyword">from</span> <span class="hljs-string">"vite"</span>;
<span class="hljs-keyword">import</span> react <span class="hljs-keyword">from</span> <span class="hljs-string">"@vitejs/plugin-react-swc"</span>;
<span class="hljs-keyword">import</span> federation <span class="hljs-keyword">from</span> <span class="hljs-string">"@originjs/vite-plugin-federation"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> defineConfig({
  plugins: [
    react(),
    federation({
      name: <span class="hljs-string">"remote_app"</span>,
      filename: <span class="hljs-string">"remoteEntry.js"</span>,
      exposes: {
        <span class="hljs-string">"./Button"</span>: <span class="hljs-string">"./src/components/Button"</span>,
        <span class="hljs-string">"./Header"</span>: <span class="hljs-string">"./src/components/Header"</span>,
      },
      shared: [<span class="hljs-string">"react"</span>, <span class="hljs-string">"react-dom"</span>],
    }),
  ],
  build: {
    modulePreload: <span class="hljs-literal">false</span>,
    target: <span class="hljs-string">"esnext"</span>,
    minify: <span class="hljs-literal">false</span>,
    cssCodeSplit: <span class="hljs-literal">false</span>,
  },
  preview: {
    port: <span class="hljs-number">5001</span>,
    strictPort: <span class="hljs-literal">true</span>,
    cors: <span class="hljs-literal">true</span>,
  },
});
</code></pre>
<h4 id="heading-explanation-of-vite-configuration">Explanation of Vite Configuration:</h4>
<p>1. <code>plugins</code>: array where you tell Vite which <strong>plugins</strong> to use when it:</p>
<ul>
<li><p>Runs your dev server</p>
</li>
<li><p>Builds your production bundle</p>
</li>
</ul>
<p>Each plugin is basically a little (or big) piece of code that hooks into Vite’s build pipeline to add extra features – for example:</p>
<ul>
<li><p>Adding React JSX/TSX support (<code>@vitejs/plugin-react</code>)</p>
</li>
<li><p>Enabling Module Federation (<code>@originjs/vite-plugin-federation</code>)</p>
</li>
</ul>
<p>2. <code>build</code>: Controls how Vite produces the production build. You don’t need to worry too much about this for the purpose of this tutorial.</p>
<p>3. <code>preview</code>: Controls how the application is served / previewed:</p>
<ul>
<li><p>Useful in microfrontend setups where a fixed port and CORS enabled are needed so other apps can fetch your remote modules.</p>
</li>
<li><p><code>strictPort: true</code> ensures predictable networking – avoids “it works on my machine” issues with random ports.</p>
</li>
</ul>
<h2 id="heading-the-vite-module-federation-plugin">The Vite Module Federation Plugin</h2>
<p>You need to configure your Vite Module Federation plugin to inform it what components are to be consumed and exposed. Let’s take a look at the configured properties:</p>
<ul>
<li><p><code>name</code>: The unique identifier for your remote application in a Module Federation setup. This is the name other applications (hosts) will use when they declare your app as a remote.</p>
</li>
<li><p><code>filename</code>: This is the name of the file that your host will load when it tries to retrieve your exposed modules.</p>
</li>
<li><p><code>exposes</code>: A mapping of public module names → local file paths. This is how you decide which parts of your code are available to be consumed remotely.</p>
</li>
</ul>
<pre><code class="lang-typescript">exposes: {
  <span class="hljs-string">"./Button"</span>: <span class="hljs-string">"./src/components/Button"</span>,
  <span class="hljs-string">"./Header"</span>: <span class="hljs-string">"./src/components/Header"</span>
}
</code></pre>
<p>The <strong>key</strong> (<code>"./Button"</code>) is the <strong>public module name</strong> – the name that other apps (the host) will use when importing the module from your remote.</p>
<h4 id="heading-how-it-works">How It Works:</h4>
<ul>
<li><p><strong>Key</strong> (<code>"./Button"</code>) is the exposed identifier other apps can request.</p>
</li>
<li><p><strong>Value</strong> (<code>"./src/components/Button"</code>) is the actual file path inside your project.</p>
</li>
</ul>
<p>For example, if your remote’s <code>name</code> is <code>"remote_app"</code>, the host can import like this:</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">import</span> Button <span class="hljs-keyword">from</span> <span class="hljs-string">"remote_app/Button"</span>;
</code></pre>
<p>Under the hood, <code>"remote_app"</code> matches the <strong>remote’s</strong> <code>name</code> in its <code>federation()</code> config and <code>Button</code> matches the <code>"./Button"</code> key in <code>exposes</code>.</p>
<ul>
<li><p><code>shared</code>: dependencies that should be <strong>shared</strong> between the host and remote. It avoids shipping duplicate copies of large libraries (like React), ensuring the host and remote use the same instance – you can read more <a target="_blank" href="https://module-federation.io/configure/shared">here</a> about the possible configurations.</p>
</li>
<li><p><code>remotes</code>: A mapping of remote app names → the URL to their <code>remoteEntry.js</code> file. This tells the host where to fetch the exposed modules at runtime.</p>
</li>
</ul>
<pre><code class="lang-typescript">remotes: { remote_app: <span class="hljs-string">"http://localhost:5001/assets/remoteEntry.js"</span> }
</code></pre>
<p>The key <code>remote_app</code> must match the <code>name</code> in the remote’s config.</p>
<p>The value is the full URL to the remote’s entry file (served in dev or deployed in prod). Remember we set <code>strictPort:true</code> earlier – this is why. We need to ensure we’re pointing at the correct domain &amp; port.</p>
<h2 id="heading-key-constraints-on-exported-modules">Key Constraints On Exported Modules</h2>
<h3 id="heading-naming-constraints-and-rules">Naming constraints and rules</h3>
<p><strong>The key in</strong> <code>exposes</code> (<code>"./Button"</code>):</p>
<ul>
<li><p>Must start with <code>./</code> (per Module Federation spec).</p>
</li>
<li><p>Must be unique within the remote.</p>
</li>
<li><p>Is case-sensitive.</p>
</li>
<li><p>This is the <strong>public module path</strong> the host will request.</p>
</li>
<li><p>Doesn’t have to match the filename, but matching is a good convention for ease of reading</p>
</li>
</ul>
<p><strong>The file you point to (</strong><code>"./src/components/Button"</code>):</p>
<ul>
<li><p>Can export default, named exports, or both.</p>
</li>
<li><p>The host can import default or named exports, same as any ES module:</p>
<pre><code class="lang-typescript">  <span class="hljs-comment">// Default export</span>
  <span class="hljs-keyword">import</span> MyButton <span class="hljs-keyword">from</span> <span class="hljs-string">'remote_app/Button'</span>;

  <span class="hljs-comment">// Named export</span>
  <span class="hljs-keyword">import</span> { SpecialButton } <span class="hljs-keyword">from</span> <span class="hljs-string">'remote_app/Button'</span>;
</code></pre>
</li>
</ul>
<p><strong>The import name:</strong></p>
<ul>
<li><p>Completely up to the host developer when importing a <strong>default</strong> export.</p>
</li>
<li><p>Must match exactly for <strong>named</strong> exports.</p>
</li>
</ul>
<h2 id="heading-how-to-create-the-remote-components">How to Create the Remote Components</h2>
<p>Ok, so you’ve created your project structure, and you’ve setup your <code>vite.config.ts</code> file to allow for exposing and consuming your shared assets. Next you will create the remote components.</p>
<h3 id="heading-button-component">Button Component</h3>
<p>Let’s say you want to create a button component which will be shared across all your host applications, as you want to keep consistency. You can do this as below:</p>
<p>Navigate to the <code>remote-app</code> folder and create a new file called <code>Button.tsx</code> in <code>src/components</code> this will ensure it matches the configured federation plugin.</p>
<pre><code class="lang-typescript"><span class="hljs-comment">// remote - ./src/components/Button.tsx</span>
<span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;

<span class="hljs-keyword">interface</span> ButtonProps {
  text: <span class="hljs-built_in">string</span>;
  onClick?: <span class="hljs-function">() =&gt;</span> <span class="hljs-built_in">void</span>;
}

<span class="hljs-keyword">const</span> Button: React.FC&lt;ButtonProps&gt; = <span class="hljs-function">(<span class="hljs-params">{ text, onClick }</span>) =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    &lt;button
      onClick={onClick}
      className=<span class="hljs-string">"px-4 py-2 bg-green-500 text-white rounded hover:bg-green-600 hover:cursor-pointer"</span>
    &gt;
      {text}
    &lt;/button&gt;
  );
};

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Button;
</code></pre>
<p>You now have a re-usable <code>Button</code> component which has some base styling but allows for configuration of what the button does by passing in a <code>onClick()</code> argument.</p>
<h3 id="heading-header-component">Header Component</h3>
<p>Sticking with the theme of consistency, you want to create a <code>&lt;header/&gt;</code> component which you can use on all your organisation’s websites, ensuring a themed appearance on all applications.</p>
<p>Like before, create a <code>Header.tsx</code> file within <code>src/components/</code>, and paste in the following code:</p>
<pre><code class="lang-typescript"><span class="hljs-comment">// remote - .src/components/Header.tsx</span>
<span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;

<span class="hljs-keyword">const</span> Header: React.FC = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    &lt;header className=<span class="hljs-string">"bg-gray-800 text-white p-4"</span>&gt;
      &lt;h1 className=<span class="hljs-string">"text-2xl"</span>&gt;Remote App Header&lt;/h1&gt;
      &lt;p className=<span class="hljs-string">"text-white"</span>&gt;Hi, Grant&lt;/p&gt;
    &lt;/header&gt;
  );
};

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Header;
</code></pre>
<p>I’ve kept it simple, as this tutorial is for proof of concept purposes rather than aesthetic / real-world components.</p>
<h2 id="heading-how-to-consume-the-remote-components-within-the-host">How to Consume the Remote Components Within the Host</h2>
<p>You have your remote components created, so next you need to get them into your host application and begin using them. This is quite simple now that you’ve already setup your <code>vite.config.ts</code>.</p>
<p>You <strong>could</strong> import the components into your <code>App.tsx</code>, but this is not best practice as it can bloat your App.tsx (entry point component). I’ve opted to create a <code>RemoteWrapperComponent</code> which pulls in the remote components and handles the business logic.</p>
<p><code>RemoteComponentWrapper</code><strong>:</strong></p>
<p>Create a file called <code>RemoteComponentWrapper.tsx</code> in <code>src/components</code>, pasting the following code:</p>
<pre><code class="lang-typescript"><span class="hljs-comment">// host - ./src/components/RemoteComponentWrapper.tsx</span>
<span class="hljs-keyword">import</span> React, { Suspense } <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;

<span class="hljs-keyword">const</span> RemoteHeader = React.lazy(<span class="hljs-function">() =&gt;</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">"remote_app/Header"</span>));
<span class="hljs-keyword">const</span> RemoteButton = React.lazy(<span class="hljs-function">() =&gt;</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">"remote_app/Button"</span>));

<span class="hljs-keyword">const</span> LoadingSpinner = <span class="hljs-function">() =&gt;</span> (
  &lt;div className=<span class="hljs-string">"flex justify-center p-4"</span>&gt;
    &lt;div className=<span class="hljs-string">"animate-spin rounded-full h-8 w-8 border-b-2 border-gray-900"</span>&gt;&lt;/div&gt;
  &lt;/div&gt;
);

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> RemoteComponentWrapper = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    &lt;div className=<span class="hljs-string">"p-4"</span>&gt;
      &lt;Suspense fallback={&lt;LoadingSpinner /&gt;}&gt;
        &lt;RemoteHeader /&gt;
      &lt;/Suspense&gt;

      &lt;div className=<span class="hljs-string">"mt-4"</span>&gt;
        &lt;Suspense fallback={&lt;LoadingSpinner /&gt;}&gt;
          &lt;RemoteButton
            text=<span class="hljs-string">"Remote Button"</span>
            onClick={<span class="hljs-function">() =&gt;</span>
              alert(
                <span class="hljs-string">"Well done you've imported the MF remote component successfully"</span>
              )
            }
          /&gt;
        &lt;/Suspense&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  );
};
</code></pre>
<p>This component acts as a wrapper, handling some very simple business logic such as loading your remote components, handling a loading spinner whilst waiting for the remote, and passing your <code>onClick</code> event to the remote button.</p>
<h3 id="heading-why-use-reactlazy">Why Use <code>React.lazy()</code>?</h3>
<p>The import with <code>React.lazy</code> isn’t required for Module Federation components – it’s more of a best practice for React apps when the remote module is:</p>
<ul>
<li><p>Loaded asynchronously at runtime, which Module Federation remotes almost always are</p>
</li>
<li><p>You want React to handle the loading state and code-splitting gracefully – shown using the <code>Suspense</code> component</p>
</li>
</ul>
<p><code>React.lazy</code> + <code>&lt;Suspense&gt;</code> gives React a built-in way to pause rendering until the component is ready. Without it, you’d need manual loading state handling.</p>
<p>It also keeps your components looking “normal.” With React.Lazy, <code>&lt;RemoteHeader/&gt;</code> is just another component in your JSX.</p>
<p>Without it, you’d need something like:</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">const</span> [Header, setHeader] = useState(<span class="hljs-literal">null</span>);

useEffect(<span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">import</span>(<span class="hljs-string">"remote_app/Header"</span>).then(<span class="hljs-function"><span class="hljs-params">m</span> =&gt;</span> setHeader(<span class="hljs-function">() =&gt;</span> m.default));
}, []);

<span class="hljs-keyword">return</span> Header ? &lt;Header /&gt; : &lt;LoadingSpinner /&gt;;
</code></pre>
<p>…which is messier and repeats for every remote component.</p>
<h2 id="heading-how-to-handle-typescript-errors">How to Handle TypeScript Errors</h2>
<p>Inside your <code>RemoteWrapperComponent</code> you’re going to see the following error on your <code>Button</code> and <code>Header</code> imports.</p>
<blockquote>
<p>Cannot find module 'remote_app/Button' or its corresponding type declarations.ts (2307)</p>
</blockquote>
<p>You get this error because the remote modules are not defined with types, so both your remote and your host doesn’t know what this imported component is, nor does it know its structure (a key part to TypeScript development).</p>
<p>To fix this you will need to provide your host app with custom types.</p>
<h3 id="heading-add-a-type-declaration-file">Add a Type Declaration File</h3>
<p>A type declaration file (if you’re unaware of them) has a <code>.d.ts</code> suffix.</p>
<p>Within your host app, create a file in <code>src/types</code> called <code>remote-app.d.ts</code>. Naming the file in this way lets us know the declarations within are related to the <em>remote-app</em>. This is useful especially when consuming multiple remotes.</p>
<p>Copy and paste the following declarations into your <code>remote-app.d.ts</code> file:</p>
<pre><code class="lang-typescript"><span class="hljs-comment">// host - ./src/types/remote.d.ts</span>
<span class="hljs-keyword">declare</span> <span class="hljs-keyword">module</span> "remote_app/Button" {
  <span class="hljs-keyword">const</span> Button: React.FC&lt;{
    text: <span class="hljs-built_in">string</span>;
    onClick?: <span class="hljs-function">() =&gt;</span> <span class="hljs-built_in">void</span>;
  }&gt;;
  <span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Button;
}

<span class="hljs-keyword">declare</span> <span class="hljs-keyword">module</span> "remote_app/Header" {
  <span class="hljs-keyword">const</span> Header: React.FC;
  <span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Header;
}
</code></pre>
<p>Now if you return to your <code>RemoteWrapperComponent</code> your errors should be gone. If they aren’t, restart your IDE (in VS Code you can open your command palette and select <code>Restart Typescript Server</code>).</p>
<h2 id="heading-how-to-add-remotewrappercomponent-to-apptsx">How to Add <code>RemoteWrapperComponent</code> to App.tsx</h2>
<p>Import the <code>RemoteWrapperComponent</code> into App.tsx.</p>
<p>I’ve removed all the boilerplate code and replaced with some basic styling to allow us to easily see what is the host, and what are the remote components.</p>
<p>Copy and paste the following code into your host <code>App.tsx</code>:</p>
<pre><code class="lang-typescript"><span class="hljs-comment">// host - ./src/App.tsx</span>
<span class="hljs-keyword">import</span> viteLogo <span class="hljs-keyword">from</span> <span class="hljs-string">"/vite.svg"</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">"./App.css"</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">"./index.css"</span>;
<span class="hljs-keyword">import</span> { RemoteComponentWrapper } <span class="hljs-keyword">from</span> <span class="hljs-string">"./components/RemoteComponentWrapper"</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    &lt;&gt;
      &lt;div className=<span class="hljs-string">"px-6 border-2"</span>&gt;
        &lt;div className=<span class="hljs-string">"flex justify-center items-center"</span>&gt;
          &lt;img src={viteLogo} alt=<span class="hljs-string">"Example"</span> /&gt;
        &lt;/div&gt;
        &lt;h1 className=<span class="hljs-string">"text-2xl"</span>&gt;Host Application&lt;/h1&gt;
        &lt;p&gt;
          {<span class="hljs-string">" "</span>}
          Welcome to the Host application, below are the components pulled <span class="hljs-keyword">from</span>
          the remote application
        &lt;/p&gt;
        &lt;RemoteComponentWrapper /&gt;
      &lt;/div&gt;
    &lt;/&gt;
  );
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App;
</code></pre>
<h2 id="heading-how-to-serve-the-remote-app-and-run-your-host">How to Serve the Remote App and Run Your Host</h2>
<p>Due to how Vite works, you need to build the application before you preview / serve the application.</p>
<p>Make sure that your <code>package.json</code> file’s scripts block looks like this:</p>
<pre><code class="lang-json"># remote-app
<span class="hljs-string">"scripts"</span>: {
    <span class="hljs-attr">"dev"</span>: <span class="hljs-string">"vite"</span>,
    <span class="hljs-attr">"build"</span>: <span class="hljs-string">"tsc -b &amp;&amp; vite build"</span>,
    <span class="hljs-attr">"lint"</span>: <span class="hljs-string">"eslint ."</span>,
    <span class="hljs-attr">"preview"</span>: <span class="hljs-string">"vite preview --port 5001 --strictPort"</span>,
    <span class="hljs-attr">"serve"</span>: <span class="hljs-string">"npm run build &amp;&amp; npm run preview"</span>
  },

# host-app
<span class="hljs-string">"scripts"</span>: {
    <span class="hljs-attr">"dev"</span>: <span class="hljs-string">"vite"</span>,
    <span class="hljs-attr">"build"</span>: <span class="hljs-string">"tsc -b &amp;&amp; vite build"</span>,
    <span class="hljs-attr">"lint"</span>: <span class="hljs-string">"eslint ."</span>,
    <span class="hljs-attr">"preview"</span>: <span class="hljs-string">"vite preview --port 5000 --strictPort"</span>,
    <span class="hljs-attr">"serve"</span>: <span class="hljs-string">"npm run build &amp;&amp; npm run preview"</span>
  },
</code></pre>
<p>In your terminal run:</p>
<pre><code class="lang-bash"><span class="hljs-built_in">cd</span> ./remote-app

<span class="hljs-comment"># run a build, and run / load the app on port 5000</span>
npm run serve
</code></pre>
<p>You should see something like this:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1755207471886/c64b29ef-3f95-44e5-903b-896f7a3a36fc.png" alt="screenshot showing output from terminal, port 5001 running remote app" class="image--center mx-auto" width="918" height="314" loading="lazy"></p>
<p>Now you can run the host application by doing the same:</p>
<pre><code class="lang-bash"><span class="hljs-comment"># change to the host-app in another terminal </span>
<span class="hljs-built_in">cd</span> ../host-app

<span class="hljs-comment"># build and run the application</span>
npm run serve
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1755207573717/77ab133f-ac2e-4f51-8152-3de7200c067c.png" alt="screenshot showing the host-app running on port 5000" class="image--center mx-auto" width="868" height="302" loading="lazy"></p>
<p>If you open the <code>localhost:5000</code> you should now see your host application with the remote components:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1756078239732/775ab6ec-4566-44cd-ac85-66c37a9001ca.png" alt="image: shows the finished host app consuming the remote components" class="image--center mx-auto" width="2524" height="646" loading="lazy"></p>
<p>Next, if you click the button, you can see that it shows the message you configured from within the <code>RemoteWrapperComponent</code>:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1756078270491/65a72ab8-1cfa-4298-be02-68d0528fd50f.png" alt="image: shows alert modal with provided message after clicking remote button" class="image--center mx-auto" width="1948" height="728" loading="lazy"></p>
<h2 id="heading-the-true-power-of-micro-frontends">The True Power of Micro Frontends</h2>
<p>The true power of micro frontends lies in their ability to update the remote components, without the need to rebuild the host. To fully demonstrate this, keep the host running, and update the <code>Button</code> component on your <code>remote-app</code>.</p>
<p>Let’s update the remote components. Use the below code to update both the <code>Button</code> and <code>Header</code> components:</p>
<pre><code class="lang-typescript"><span class="hljs-comment">// remote - ./src/components/Header.tsx</span>
<span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;

<span class="hljs-keyword">const</span> Header: React.FC = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    &lt;header className=<span class="hljs-string">"bg-gray-800 text-white p-4"</span>&gt;
      &lt;h1 className=<span class="hljs-string">"text-2xl"</span>&gt;Updated Remote App Header&lt;/h1&gt;
      &lt;p className=<span class="hljs-string">"text-white"</span>&gt;Hi, Grant&lt;/p&gt;
    &lt;/header&gt;
  );
};

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Header;

<span class="hljs-comment">// remote - ./src/components/Button.tsx</span>
<span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;

<span class="hljs-keyword">interface</span> ButtonProps {
  text: <span class="hljs-built_in">string</span>;
  onClick?: <span class="hljs-function">() =&gt;</span> <span class="hljs-built_in">void</span>;
}

<span class="hljs-keyword">const</span> Button: React.FC&lt;ButtonProps&gt; = <span class="hljs-function">(<span class="hljs-params">{ text, onClick }</span>) =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    &lt;button
      onClick={onClick}
      className=<span class="hljs-string">"px-4 py-2 bg-red-500 text-white rounded hover:bg-red-600 hover:cursor-pointer"</span>
    &gt;
      {text}
    &lt;/button&gt;
  );
};

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Button;
</code></pre>
<p>Once you’ve updated the remote components, run the following command in your <code>remote-app</code> folder:</p>
<pre><code class="lang-bash">npm run serve
</code></pre>
<p>Next, refresh your host app in the browser and you will see the updated app:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1756078980661/02ba0655-a18c-460c-939b-6fa95b4149b7.png" alt="image: shows host application with updated remote compnents" class="image--center mx-auto" width="2514" height="664" loading="lazy"></p>
<p>The update to the remote components is visible immediately, without restarting or rebuilding the host app. This highlights a key benefit of micro frontends: shared components are fetched from their own server via the <code>remote.js</code> file, enabling independent updates.</p>
<h2 id="heading-final-thoughts">Final Thoughts</h2>
<p>You've successfully built and deployed a micro frontend architecture – congrats! This basic implementation demonstrates the true power of Module Federation and the ability to update shared components without needing to rebuild and redeploy the entire host application.</p>
<p>This independence can dramatically accelerate development cycles and empower teams to work more autonomously.</p>
<p>I hope you’ve learned something from this article, and as always for more tutorials and discussions, connect with me on <a target="_blank" href="https://x.com/grantdotdev">twitter/x</a>.</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
