The Complete Guide to Micro Frontend with React.js for 2022
Tags: react, mircofrontend, javascript, webpack
Published: 2022-03-17 11:00:52


![Micro Frontend with React.js and Module federation | Rahul Sharma(DevsMitra)](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j2ejrysgwoo72lz4bt69.png "Micro Frontend with React.js and Module federation | Rahul Sharma(DevsMitra)")
### What is a micro frontend?

The Micro Frontend style of architecture design does for the frontend of an application what microservices do for the backend, breaking monolithic structures into smaller components that can then be assembled on a single page.

[![Watch the video](https://img.youtube.com/vi/u4Z9INmHhCM/0.jpg)](https://youtu.be/u4Z9INmHhCM?autoplay=1)

### Pros and cons of micro frontend:
#### Pros:

- A micro frontend is more modular and reusable.
- A micro frontend is more scalable.
- The micro frontend is more maintainable.
- Independent and Faster development.
- Testing separate applications are easy.
- Different front-end technologies can be used for different 
projects(like React, Angular, Vue.js, etc).

#### Cons:

- Testing the entire application is not easy.
- Sharing code, state(data), etc is not easy.


### Module federation ([Module Federation | webpack](https://webpack.js.org/concepts/module-federation))
Module Federation allows a JavaScript application to dynamically load code from another application and in the process, share dependencies. If an application consuming a federated module does not have a dependency needed by the federated code, Webpack will download the missing dependency from that federated build origin.


#### Prerequisite:

- Make sure you have installed the latest Node.js
- Basic knowledge of React.js and Webpack 5+.
- Code editor

### I'll create 2 apps in this article:
- **First:** Container app that will be used as a base for the micro frontends.
- **Second:** The counter app that will get rendered inside the container app.

Let’s start with setting up the environment. I’m using [create-mf-app](https://github.com/jherr/create-mf-app) to create a react app.


## Container App
#### Installation
Run the following npx command on a terminal to install and bootstrap the application using “create-mf-app”. Let’s call our application “container”

```javascript
npx create-mf-app
```

![Micro Frontend with React.js and Module federation | Rahul Sharma(DevsMitra)](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1lkhunrsvgvq5erb2rl6.png "Micro Frontend with React.js and Module federation | Rahul Sharma(DevsMitra)")
After completing the first step, The directory initially looks something like this:

![Micro Frontend with React.js and Module federation | Rahul Sharma(DevsMitra)](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vpaavvn0natpsdthy2al.png "Micro Frontend with React.js and Module federation | Rahul Sharma(DevsMitra)")

I will not deep dive into the folder structure, It’s similar to the create-react-app folder structure.

#### NOTE: Only difference here is the index.js file, Which loads the app dynamically.

```javascript
import('./App');
```

Let’s quickly create another app called Counter using the same steps as above.


## Counter App

```javascript
npx create-mf-app
```

![Micro Frontend with React.js and Module federation | Rahul Sharma(DevsMitra)](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zh2wywbt8jx3rhex311i.png "Micro Frontend with React.js and Module federation | Rahul Sharma(DevsMitra)")

Inside the counter app, I have created a Counter component in the components folder.

**src/components/Counter.jsx**

```javascript
import React, { useState } from "react";
export const Counter = () => {
    const [count, setCount] = useState(0);
    const onIncrement = () => setCount(count + 1);
    const onDecrement = () => setCount(count - 1);
    return (
      <div>
        <h1>Counter App</h1>
        <p>Current count: <strong>{count}</strong></p>
        <button onClick={onIncrement}>+</button>
        <button onClick={onDecrement}>-</button>
      </div>
    );
}
```

Let’s update the **webpack.config.js** file inside the Counter app. Add ModuleFederationPlugin to the plugins array with the following configuration:

**webpack.config.js**

```javascript
const HtmlWebPackPlugin = require("html-webpack-plugin");
const ModuleFederationPlugin = require("webpack/lib/container/ModuleFederationPlugin");
const deps = require("./package.json").dependencies;
module.exports = {
  output: {
    publicPath: "http://localhost:8081/",
  },
  resolve: {
    extensions: [".tsx", ".ts", ".jsx", ".js", ".json"],
  },
  devServer: {
    port: 8081,
    historyApiFallback: true,
  },
  module: {
    rules: [
      {
        test: /\.m?js/,
        type: "javascript/auto",
        resolve: {
          fullySpecified: false,
        },
      },
      {
        test: /\.(css|s[ac]ss)$/i,
        use: ["style-loader", "css-loader", "postcss-loader"],
      },
      {
        test: /\.(ts|tsx|js|jsx)$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader",
        },
      },
    ],
  },
  plugins: [ // This is important part
    new ModuleFederationPlugin({
      name: "counter",
      filename: "remoteEntry.js",
      remotes: {},
      exposes: {
        "./Counter": "./src/components/Counter",
      },
      shared: {
        ...deps,
        react: {
          singleton: true,
          requiredVersion: deps.react,
        },
        "react-dom": {
          singleton: true,
          requiredVersion: deps["react-dom"],
        },
      },
    }),
    new HtmlWebPackPlugin({
      template: "./src/index.html",
    }),
  ],
};
```

#### Let’s understand what each option is:
1. **name:** Name of the remote app
2. **filename:** Entry point(remoteEntry.js) for the counter app.
3. **remotes:** Add remotes entry here (relevant for the container)
4. **exposes:** All the component names that you want to expose to the container app.
5. **shared:** container all the dependencies that you want to share between the container and the counter app.




Let’s update the **webpack.config.js** file inside the Container app.

**webpack.config.js**

```javascript
const HtmlWebPackPlugin = require("html-webpack-plugin");
const ModuleFederationPlugin = require("webpack/lib/container/ModuleFederationPlugin");
const deps = require("./package.json").dependencies;
module.exports = {
  output: {
    publicPath: "http://localhost:8080/",
  },
  resolve: {
    extensions: [".tsx", ".ts", ".jsx", ".js", ".json"],
  },
  devServer: {
    port: 8080,
    historyApiFallback: true,
  },
  module: {
    rules: [
      {
        test: /\.m?js/,
        type: "javascript/auto",
        resolve: {
          fullySpecified: false,
        },
      },
      {
        test: /\.(css|s[ac]ss)$/i,
        use: ["style-loader", "css-loader", "postcss-loader"],
      },
      {
        test: /\.(ts|tsx|js|jsx)$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader",
        },
      },
    ],
  },
  plugins: [ // This is important part
    new ModuleFederationPlugin({
      name: "container",
      filename: "remoteEntry.js",
      remotes: {
        counter: "counter@http://localhost:8081/remoteEntry.js",
      },
      exposes: {},
      shared: {
        ...deps,
        react: {
          singleton: true,
          requiredVersion: deps.react,
        },
        "react-dom": {
          singleton: true,
          requiredVersion: deps["react-dom"],
        },
      },
    }),
    new HtmlWebPackPlugin({
      template: "./src/index.html",
    }),
  ],
};
```

_**NOTE:** The remote objects will have to define all the entry points exposed from remote apps, remotes entry has the following structure:_

```json
{ "app-name": "name@<remote-host>/remoteEntry.js" }
```

**src/App.jsx**

```javascript
import React from "react";
import ReactDOM from "react-dom";
import { Counter } from 'counter/Counter';
import "./index.css";
const App = () => (
  <div className="container">
    <h1>Container App</h1>
    <Counter /> // Micro frontend app
  </div>
);
ReactDOM.render(<App />, document.getElementById("app"));
```

> Time to run both the apps

![Micro Frontend with React.js and Module federation | Rahul Sharma(DevsMitra)](https://media.giphy.com/media/YOkjsXE1daFapAo1L5/giphy.gif)

# Counter App

![Micro Frontend with React.js and Module federation | Rahul Sharma(DevsMitra)](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rr4i19xeu1w6kc7yhlq6.png "Micro Frontend with React.js and Module federation | Rahul Sharma(DevsMitra)")

# Container App

![Micro Frontend with React.js and Module federation | Rahul Sharma(DevsMitra)](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eozf75fwz41qpg2f2mfo.png "Micro Frontend with React.js and Module federation | Rahul Sharma(DevsMitra)")

Github Repo:
https://github.com/devsmitra/micro

Reference:
https://github.com/jherr/create-mf-app

Got any questions or additional? please leave a comment.

Thank you for reading 😊

### Must Read If you haven't

{% link https://dev.to/devsmitra/no-more-redux-action-creators-functions-30j %}
{% link https://dev.to/devsmitra/how-to-cancel-javascript-api-request-with-abortcontroller-36cm %}
{% link https://dev.to/devsmitra/13-typescript-utility-a-cheat-sheet-for-developer-ab3 %}
{% link https://dev.to/devsmitra/how-to-solve-rest-api-routing-problem-with-decorators-5b3n %}


### Catch me on
{% user devsmitra %}
<p>
{% cta https://www.youtube.com/@devsmitra?sub_confirmation=1 %} Youtube {% endcta %}
{% cta https://github.com/devsmitra %} Github {% endcta %}
{% cta https://www.linkedin.com/in/rahul-sharma-18b8b374 %} LinkedIn {% endcta %}
{% cta https://devsmitra.medium.com %} Medium {% endcta %}
{% cta https://stackblitz.com/@devsmitra %} Stackblitz {% endcta %} {% cta https://devsmitra.hashnode.dev %} Hashnode {% endcta %}
{% cta https://hackernoon.com/u/devsmitra %} HackerNoon {% endcta %}
</p>