Let's review Nx Monorepo

Akshay NazareAkshay Nazare
3 min read

An out-of-box concept that helps you manage codebases of any scale.This article will help you gain primary knowledge about Nx Monorepo.Images and link references are taken from the official documentation nx.dev


Table of contents

  1. Traditional methods

  2. Where exactly Nx Monorepo comes into play ?

  3. Cache mechanism of Nx monorepo

  4. Plugins

  5. Basic commands

Traditional methods ™️

Now, let's consider a scenario where you want to create a full-stack project using the following tech stack:

Frontend: React framework application
Backend: Node.js (Express.js) application

To do this, you would need to create separate folders: one for managing the frontend UI and another for managing the backend APIs of the Express application. Handling both applications separately will take a lot of your time. Additionally, to run the project, you would need to execute two different commands:

npm run dev*nodemon index.js*

and We often rebuild and retest the same code repeatedly which becomes an overhead for our RAM and cpu so now let’s dive into

Where exactly Nx Monorepo comes into play and how does cache mechanism works ?

The remote caching mechanism works by:

  1. Computation Hashing: After Nx computes the hash for a task, it checks if it ran this exact computation before - first locally, then remotely if a remote cache is configured

    • Before running any cacheable task, Nx computes its computation hash. As long as the computation hash is the same, the output of running the task is the same.

    • After Nx computes the hash for a task, it then checks if it ran this exact computation before. First, it checks locally, and then if it is missing, and if a remote cache is configured, it checks remotely. If a matching computation is found, Nx retrieves and replays it. This includes restoring files.

    • Nx places the right files in the right folders and prints the terminal output. From the user's point of view, the command ran the same, just a lot faster.

computation-hashing

  1. If a matching computation is found, Nx retrieves and replays it from scratch.

  2. Team-wide Benefits: The biggest benefit comes from sharing this cache across your team and in CI in collaborative environments as it ensures consistent, efficient task execution across team members.

    cache

  3. Resource Savings: Nx Replay allows developers to avoid re-running expensive tasks by retrieving cached results, which significantly improves build and test performance.

Plugins

  • Nx Monorepo provides you a wide range of plugins so that you can integrate different framework applications into your Nx-Workspace

It provides the plugins with in-built documentation for you to setup it in your local directory

Basic commands to get you started with Nx-Monorepo ( Windows )

To install Nx CLI on your machine, choose one of the following methods based on your operating system and package manager. You can also use npx to run the Nx CLI without installing it globally.

npm add --global nx

Adding Nx to Your Repository

To add Nx to an existing repository, run:

nx init

Starter Repository

To create a starter repository, you can use the create-nx-workspace command. This will create a new Nx workspace with a default configuration and example applications.

npx create-nx-workspace@latest

Update Nx

When you update Nx, Nx will also automatically update your dependencies if you have an Nx plugin installed for that dependency. To update Nx, run:

nx migrate latest

This will create a migrations.json file with any update scripts that need to be run. Run them with:

nx migrate --run-migrations
0
Subscribe to my newsletter

Read articles from Akshay Nazare directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Akshay Nazare
Akshay Nazare

I am a full-stack developer keen to learn new technolgies and also strive upon mastering one