Micro front with NX

Shubham chaturvedi
3 min readAug 8, 2023

--

Certainly! “Micro Frontends with NX” refers to using the Nx workspace and tools to develop a micro frontend architecture. Nx is a set of extensible dev tools for monorepos, which can help manage and develop multiple applications and libraries within a single codebase more efficiently.

In the context of micro frontends, Nx allows you to create and manage separate frontend modules that can be developed and deployed independently, yet work together seamlessly as a single application. Each module could represent a distinct feature or functionality, and Nx helps you manage their dependencies, builds, testing, and more in an efficient manner.

By using Nx with a micro frontend architecture, you can achieve benefits such as:

Isolation: Each micro frontend can be developed, tested, and deployed independently, reducing the risk of affecting other parts of the application.

Decoupling: Teams can work on different micro frontends, allowing for better separation of concerns and specialization.

Reusability: Nx’s workspace structure allows for sharing libraries and components across micro frontends, promoting code reuse.

Scalability: As your application grows, micro frontends can make it easier to manage complexity and scale development efforts.

Flexibility: Micro frontends can be developed using different technologies and frameworks, enabling teams to choose the best tools for their specific needs.

Nx provides out-of-the-box Module Federation support to both React and Angular. The Micro Frontend (MFE) architecture builds on top of Module Federation by providing independent deployability.

Architectural overview

With MFE architecture, a large application is split into:

  1. A single Host application that references external.
  2. Remote applications, which handle a single domain or feature.

Generating applications

The generator for MFEs is the same as with the basic Module Federation. You can use nx g host to create a new host application, and nx g remote for remote applications.

#a React
nx g @nrwl/react:host main --remotes=shop,cart
nx g @nrwl/react:remote about --host=main
#b Angular
nx g @nrwl/angular:host main --remotes=shop,cart
nx g @nrwl/angular:remote about --host=main

That is! You can now run nx serve main to develop on the main application while keeping all remotes static. To develop on one or more remote applications, pass the — devRemotesoption.

e.g. nx serve main — -devRemotes=cart,shop.Nx provides out-of-the-box Module Federation support to both React and Angular. The Micro Frontend (MFE) architecture builds on top of Module Federation by providing independent deployability.

Architectural overview

With MFE architecture, a large application is split into:

  1. A single Host application that references external.
  2. Remote applications, which handle a single domain or feature

Understand this workspace

Nx Micro frontend directory setup.

Run npx nx graph to see a diagram of the dependencies of the projects.

Project Flow

Run npm run start to serve the project locally. Here you will see the main app will be served on port 4200 and all the other remotes will run on incrementally numbered ports.

Communication

Way to exchange data between micro-apps:

--

--