Monorepo Overview, Evolution, Pros & Cons, Pitfalls, and Tool Selection
This article explains what a monorepo is, traces its evolution from single‑repo monoliths to multi‑repo and back to a single repository with many modules, compares its advantages and disadvantages, lists common pitfalls, and evaluates major tooling options such as Turborepo, Rush, Nx, Lerna, Yarn and pnpm for different project sizes.
1. Monorepo Introduction
Monorepo is a code‑management strategy where multiple projects live in a single repository, simplifying code sharing, version control, building and deployment, and fostering an open, transparent, and collaborative culture. Companies like Google, Facebook and Microsoft use it extensively.
2. Monorepo Evolution
Stage 1 – Single‑repo monolith: All code resides in one Git repository, which grows into a massive monolith as the business expands, causing slower builds.
Stage 2 – Multi‑repo (MultiRepo): Projects are split into multiple repositories, each handling its own build, test and release, reducing monolith complexity but increasing management overhead.
Stage 3 – Single‑repo with many modules (MonoRepo): Multiple projects are merged back into one repository, sharing configuration and enabling fast module sharing, while still keeping logical separation.
3. Monorepo Advantages & Disadvantages
Scenario
MultiRepo
MonoRepo
Code Visibility
✅ Isolated per repo; ❌ Dependency debugging requires locating the package.
✅ Easy to see changes across projects; ❌ Higher risk of accidental changes by non‑owners.
Dependency Management
❌ Duplicate node_modules per repo, high disk usage.
✅ Single top‑level install saves disk space.
Code Permissions
✅ Per‑repo isolation prevents accidental edits.
❌ No fine‑grained permission control; a problem in one project can affect all.
Development Iteration
✅ Small repo size, clear modules.
❌ Frequent repo switching;
npm linkis cumbersome; version conflicts may arise.
✅ Full view of related projects; high code reuse.
❌ Large repo size (several GB);
git clonetakes long; but tools can automate
npm link.
Engineering Configuration
❌ Inconsistent build configs across repos.
✅ Unified configuration, consistent code style.
Build & Deployment
❌ Manual ordering across repos, low efficiency.
✅ Tools can prioritize builds and deploy all with a single command.
4. Monorepo Use Cases
For medium‑to‑large, multi‑module projects, a MonoRepo brings benefits in development speed, collaboration, and code consistency.
5. Monorepo Pitfalls
5.1 Ghost Dependencies
When a package uses a dependency that is not declared in its package.json , the dependency may disappear after other projects stop using it, causing runtime errors. Using pnpm eliminates this issue.
5.2 Long Dependency Installation Time
Each project in a MonoRepo has its own package.json . As the total number of dependencies grows, npm install becomes slow. Lifting common versions to the repo root and using pnpm with caching reduces the time.
5.3 Long Build & Packaging Time
When projects depend on each other, builds are often serial or full‑scale, leading to long build times. Incremental or parallel builds can mitigate this.
6. Monorepo Tool Selection
6.1 Build‑Focused Solutions
6.1.1 Turborepo
Turborepo (by Vercel) is a high‑performance build system that enables parallel task execution, caching, and remote caching.
Multiple Running Task: Parallel builds with configurable order.
Cache / Remote Cache: Local and remote caches reduce rebuild time.
Example command sequence for a typical Yarn workspace monorepo:
# When using normal yarn workspace workflow
yarn workspaces run lint
yarn workspaces run test
yarn workspaces run buildTraditional Yarn workspaces run tasks serially, causing poor performance.
6.1.2 Rush
Rush (Microsoft) is an extensible monorepo tool that focuses on build orchestration, version management, and plugin extensibility. It provides:
Parallel builds.
Plugin system for custom workflows.
Automatic changelog generation.
6.1.3 Nx
Nx (Nrwl) adds richer caching (including remote), incremental builds, parallel execution, and distributed builds via Nx Cloud.
6.2 Lightweight Solutions
6.2.1 Lerna (Full Walk‑through)
Lerna, created by Babel, excels at dependency management and versioning for many packages. Core commands:
lerna run – execute a script in each package.
lerna bootstrap – install and link dependencies.
lerna publish – detect changed packages, bump versions, generate changelogs, commit/tag, and publish to npm.
Typical workflow snippets:
# Initialize a monorepo
lerna init
# Create a new package
lerna create
[location]
# Add a dependency to all packages
lerna add module-1
# Install all dependencies (bootstrap)
lerna bootstrap
# Publish changed packages
lerna publish6.2.2 Yarn/NPM Workspaces
Yarn 1.x+ and npm support native workspaces by declaring a workspaces field in the root package.json . This lifts shared dependencies to the root node_modules , speeding up installs and reducing duplication.
6.2.3 Lerna + pnpm + Workspaces
pnpm uses a content‑addressable store and a virtual node_modules/.pnpm directory, providing ultra‑fast installs, high disk‑space efficiency, and eliminating ghost dependencies.
6.3 Comparison Table
Tool
Capability
Lerna (NPM)
NPM Workspace
Yarn Workspace
Dependency Management
Init & hoist
lerna bootstrap
npm install
yarn
Install Dependency
lerna add
npm install -w pkg
yarn workspace pkg add
Remove Dependency
—
npm uninstall -w pkg
yarn workspace pkg remove
Publish
Global script execution
lerna run --scope=pkg
npm run -w pkg
yarn workspace pkg run
Run Scripts Across Packages
lerna run
npm run --ws
yarn workspaces run
Exec Command Per Package
lerna exec
npm exec -c 'cmd' --ws
yarn workspaces foreach cmd
Version & Changelog
lerna publish
—
—
Overall recommendation: combine Yarn workspaces (for fast, hoisted installs) with Lerna (for versioning, changelog, and batch script execution). This hybrid approach offers smooth dependency handling and powerful release workflows.
6.4 Choosing the Right Tool
For lightweight monorepos, start with Lerna + pnpm workspace + lerna‑changelog. As the codebase grows and inter‑project dependencies become complex, migrate to Nx for advanced incremental and distributed builds.
7. Best Practices
Stay tuned for more tips – please like and follow for future updates.
Rare Earth Juejin Tech Community
Juejin, a tech community that helps developers grow.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.