Monorepository is a hot topic at the table. Though the concept first appeared about a decade ago, it took so many years for this tool to evolve at a large scale. You would be amazed to know that Google was among the very first companies that embraced this approach along with all its downsides.
A monorepo is a repository that contains code for many projects and packages.
With monorepo, all you have to do is to manage one repository instead of struggling with multiple different repositories. Facebook and Google present themselves as perfect examples of using Monorepo.
In this blog, we will get a brief insight about monorepo-
What is Monorepo??
Monorepo is a combination of two syllabic abbreviations, i.e. mono and repo, where ‘mono’ refers to monolithic and ‘repo’ means repository.
In unison, we’re talking about the monolithic repository which can further be defined as- A software development strategy to store all the isolated code parts from multiple projects, in a single repository.
These projects in a monorepo don’t have to be related to each other, in one way or another. But they can still be connected via other means such as dependency management tools.
Monorepo has nothing to do with monolithic apps. Unlike monolithic apps, you can store as many logical apps inside it as you need, irrespective of whether they are website applications or iOS apps.
While Git is a version control system used to solve issues around various code versions, Monorepo is a source control pattern which somewhat performs a similar function but with several projects/codebases.
You may also compare it with your ‘office work’ folder containing several files, right from documents, packages, servers, applications, etc. In non-technical language, monorepo is nothing but a folder containing the code of several projects in different files (Well, not exactly but close!)
Why Use Monorepo??
In the programming world, simplicity is everything. A good developer keeps his code as simple as he can. And monorepo is one such approach that helps you to keep your code as simple as possible.
It allows you to follow three basic rules of coding, described as-
- Keeping the code simple
- Reducing the code repetition- DRY Approach (Don’t Repeat Yourself) –
- Rule of composition — Separating out the simple objects and putting them together to form complex ones.
Other than maintaining the code-simplicity and keeping it clean & organized, it also carries some additional features, like-
- Simplified dependency management
- Changes can easily be coordinated across modules.
- Simplified organization of projects.
- Great Tools
- Easier Testing Process
- All-in-one process for lint, build, test, and release.
- Easy to make cross-project changes
Tools for Monorepo.
It’s a tool that splits up large and complex codebases into smaller and separate packages and publishes each of them on NPM independently. This whole fission is very useful and makes the code sharing process easier. It internally links all the dependencies through symlinking.
Lerna is a tool that optimizes the workflow around managing multi-package repositories with git and npm. It can also reduce the time and space requirements for numerous copies of packages in development and build environments.
Here is how you can install Lerna with npm-
$ npm install — global lerna
Create a new repository with-
$ git init lerna-repo && cd lerna-repo
Now, you can easily turn this repo into Lerna repository, using-
$ lerna init
You’ve created a lerna.repo repository which should look like this now-
Initially, yarn was not built to support monorepo but in new version, i.e. Yarn 1.0, the feature called ‘Workspaces’ was included.
It links the dependencies together which further makes your workspaces depend upon each other while using the available codes and it becomes easier to deal with them without much conflict.
The How Part.
Once you get to know about how to implement Monorepo in your project, the further part about installing, running, and managing your code in it becomes too easy.
At first, you need to clone the full repository with the
git clone command.
Install it in the root folder, using-
Now, run any of the code from the packages by any of the following commands –
yarn workspace your-package-name script-name
cd packages/app && yarn start
Add npm Libraries:
To add a package (Here, react and react-dom)-
yarn workspace your-package-name add react react-dom — dev
Remove npm Libraries:
To remove a package-
yarn workspace your-package-name remove react react-dom
To call the test script for each workspace-
yarn workspaces run test
Pros & Cons.
Everything has some pros as well as cons. The same is true in the case of monorepo. In fact, if you do some research you would find that it is facing quite a lot of criticism. Yes, there are a few downsides as well. Everything has. Flaws come complementary with everything.
Let’s have a look at its disadvantages-
- Security Issues- Allows read-access to all the stored projects which leads to security vulnerabilities.
- With the number of packages, increases the bootstrap time.
- Inconvenient for productive OSS collaboration and community building.
- Scalability challenges.
It is not platonic or something. But it’s not unreasonable either. As a matter-of-fact, it would not wrong to say that that the individuals at Google, Facebook, and Twitter must have had some strong reasons to turn to Monorepos instead of going with thousands of smaller repositories.
Rather we should see so many positive sides of monorepo, like-
- Easier Code Sharing
- Deployment Flexibility
- Team Collaboration
- Atomic Commits
- Library creation is easy.
- Large-scale code refactoring.
- Seamless working between different packages.
- Reduces friction of local development.
- Simpler code review.
With so many advantages, one can easily spare some of the drawbacks. Actually, we can expect all these issues to be resolved in the near future.