The world of web development is constantly getting bigger and developers are always trying to find a solution to help them to write more codes, build their projects faster, minimize time-wasting, make their projects available everywhere with just a few lines of code to set up the workspace. There is a sea of tools that will help with those problems and in the web development world, due to its popularity, Nx tool for Monorepos needs no further introduction.
What is a Monorepo?
A Monorepo is simply a single repository containing multiple projects with some defined relationships. It gives you visibility of your company’s entire codebase without the need to track down and quote a bunch of different repositories. Basic approach is to use your package manager like Yarn or NPM to define workspaces. These tools basically configure your project with a root-level package JSON, which then has nested workspaces, like apps and packages, that are linked back to the root-level project. A cool thing about this is that it will deduce your node modules, meaning that if you have the same package installed in multiple apps, it will only be installed once.
Why use a Monorepo?
A software company like Google maintains a lot of code, like seriously, a lot. Over 2 billion lines of code and nearly a hundred TB of data to go along with it. If they scaled, it would be hard to even comprehend it, like all the stars in the universe or all the universes in the multiverse and you have thousands of engineers around the world working on it simultaneously. But get this, they store all their code in a single Monolithic repository, and they've been doing it since the very beginning. Today, Google's Mono Repo is likely the largest code base in the world. Monorepo is great because it provides consistency, you can share things like eslint config, a UI library of web components, system utility libraries, documentation and so on.
There is one big problem with Monorepos, and that’s the fact that they are big. As the Monorepo becomes larger, there are more things to test, more things to build and a lot more artifacts to store as a result. Your editor will lag trying to process the massive git history and you’ll need a 20 minutes smoke break waiting for everything to run on the CI server after every commit. To operate a Monorepo at scale, it’s absolutely essential to have the right tooling, that’s where Nx comes in. Nx is a tool that builds a project graph by analyzing your workspace and then uses it only to rebuild what is necessary and to never run the same computation twice. Nx offers a lot of great features, it has a high-quality VS Code plugin, interactive visualizations, GitHub integrations and much more. The best part is that it is not necessary to use all of those features Nx provides. You don’t need the Nx plugin? No biggie, you don’t need it, you can only use core features and configurations to make your Monorepo easier to use, everything else is a plus. The most important thing is that Nx is fast, it uses its distributed task execution and computation caching to keep your CI time the same. T Whether you build one project or ten of them at the same time, it doesn’t matter, it is all the same.
Of course, nothing is perfect, so just like everything else, Nx has its own few negatives. Single package.json for all your projects could be a problem and also, when triggering updates, all projects are considered affected and need to be rebuilt, so keep your projects fairly updated and you’ll be okay. Some editors struggle with large monorepos, so it is recommended to use VS Code which handles the problem really well.
All in all, Monorepos can be a big mess, but with great tools such as Nx, they are easier to control, to use and to navigate through your projects. With fast build times, many plugins, nice visualizations and so on, you really cannot go wrong on this one. So go on, and make your programming and your life easier, check out Nx, and your future self will thank you for it.