What is NX and a Monorepo?
Before we dive into the world of nx we must first understand what a monorepo is.
The Wikipedia definition states the following
“In version control systems, a monorepo (“mono” meaning ‘single’ and “repo” being short for ‘repository’) is a software development strategy where code for many projects is stored in the same repository. This software engineering practice dates back to at least the early 2000s,[1] when it was known as a ‘shared codebase’”
So simply put a monorepo is just a normal repo that contains many related projects, but that comes with many advantages and disadvantages.
Advantages
- Ease of code reuse
- Simplified dependency management
- Atomic commits
- Refactoring
- Cross team collaboration
Disadvantages
- Loss of version information
- No Per-Project access control
- Scalability
- Can get messy
So what is NX, it is a smart, fast and extensible build system with first class monorepo support. It is a CLI that is designed to help you be more productive and be extensible through the use of plugins. The NX CLI is agnostic in that it can be configured to support multiple languages and tools. It already supports, through plugins, most of the common languages you might use, Java, C#, Typescript etc. The CLI allows you to break up your codebase into different projects and operate and manage the different parts.
In general NX commands can be split into 3 categories.
- Acting on code (Build, Test, Serve) – These CLI commands are an abstraction to the commands that would usually be run on a specific implementation, for example for an traditional Angular project you might run “ng serve” where in nx you would run “nx run my-angular-app:serve”, this is just a way to provide a single unified command to operate on your projects
- Modifying code (Generate, Migrate) – These are tools to help modify your codebase, add new projects, update projects etc.
- Understanding the codebase (Graph, Affected) – These are tools to help the understanding of your codebase, they help produce graphs that can show how your projects are related to each other, how and what your changes to the code base are effecting.
Ultimately what Nx provides is a tool to help manage a monorepo, it provides many helpful commands and helps to enforce a structure that ensures the Advantages of a monorepo are realised while also minimizing the downsides.
Nx provides us with many more tools to help improve our development experience outside of managing the code, but these can be the topic of a future post.
Writeup by – Darryn Hosking