Monorepo for Multiple Projects: A Practical Guide to Unified Codebase Management
This article explains how a monorepo solves the collaboration friction caused by cross-project dependencies, including delayed integration, version drift, and untraceable commits. It covers the core mechanisms, the trade-offs and limits, a five-stage adoption path, common anti-patterns, and how a monorepo fits into a cloud-based engineering stack.
Monorepo for Multiple Projects: A Practical Guide to Unified Codebase Management
1. Where the Problem Comes From: Collaboration Friction Across Projects
When a core project that runs in production is depended on by several business projects, collaboration costs rise quickly. A typical scenario: a business project finds a capability gap or a defect in the core project during iteration and needs a temporary code change, but that change is not meant to be released publicly in the near term. With a multi-repository setup, the team immediately hits three walls.
The first wall is integration delay. A change in the core project must be built, versioned, and published to an artifact repository; only then can the business project bump the dependency version, reinstall, and rebuild to see the effect. One change is split into several cross-repository operations, stretching the feedback loop to tens of minutes or even hours.
The second wall is version drift. A business project temporarily depends on an unreleased intermediate version. By the time it goes live, the core project may have moved ahead by several commits, and deciding which version is authoritative comes down to manual records and verbal sync.
The third wall is untraceable commits. A single requirement often changes both core capabilities and business logic, yet it is scattered across multiple commits in multiple repositories. Rollback, auditing, and code review are hard to perform on a per-requirement basis.
A single repository with multiple packages (commonly called a monorepo) is the structural answer to these three walls: related projects live in one repository, so source-level dependencies map directly to directory relationships inside the repository.
2. Core Mechanisms: Why Keeping Code Together Is Faster
2.1 Local Source Linking Removes the Release Wait
Inside one repository, dependencies between packages can be resolved as path references to the local workspace. After you modify the core package source, the business packages that depend on it read the new code on their next build, with no “package, upload, download” cycle. The feedback loop shrinks from hours to seconds.
A sample directory structure:
repo/├── package.json # Workspace root configuration├── packages/│ ├── core/ # Core capability package│ │ ├── src/│ │ └── package.json│ ├── service-a/ # Business package A, depends on core│ │ ├── src/│ │ └── package.json│ └── service-b/ # Business package B, depends on core│ ├── src/│ └── package.json
The root configuration declares the workspace scope, and the package manager creates the local links automatically:
{"private": true,"workspaces": ["packages/*"]}
2.2 Atomic Commits Align Requirement Boundaries with Commit Boundaries
When one requirement involves both core capabilities and business logic, it can be completed in a single commit. This yields three direct benefits: code review sees the full context at once; rollback can undo the requirement as a whole, without leaving a broken state where the core package has been rolled back but the business package still references the new interface; and the commit history naturally becomes a trustworthy record of requirements and dependency changes.
2.3 Dependency Hoisting and Unified Versions Reduce Duplication and Conflicts
Within one repository, you can lock a single version of shared dependencies, avoiding multiple copies of the same library installed across sub-packages. Once dependencies are hoisted to the workspace root, install size, build time, and the probability of dependency conflicts all go down. Version numbers of sub-packages can also be managed by a unified policy, ending debates about “which one is the latest.”
2.4 Unified Engineering Capabilities Spread Maintenance Costs
Code standards, type checking, unit tests, build pipelines, and dependency security scanning can be defined once at the root level and apply to all sub-packages. Adding a business package requires no scaffolding from scratch; existing configuration is reused directly.
3. Costs and Boundaries: When It Is Not a Good Fit
A monorepo is not a cost-free silver bullet, and its costs must be clear.
Coarser permission granularity. The repository is the smallest unit for permissions. If different sub-packages need strictly isolated access control, a single repository becomes cumbersome.
Build scope easily gets out of control. Any commit may trigger a full build, and as the number of sub-packages grows, pipeline duration degrades linearly. This is the most common reason adoption fails.
Codebase growth affects clone efficiency. As the repository grows, the initial clone and branch switching slow down, which requires repository-level capabilities such as incremental fetching to mitigate.
Higher governance requirements. Once directory boundaries, dependency direction, and naming conventions slip, a monorepo degrades into a “big ball of mud” that is harder to split apart than multiple repositories.
The decision criteria can be summarized as: whether real and frequent source-level dependencies exist among the projects; whether the team is willing to invest in unified standards and automation; and whether fine-grained per-package permission isolation is required. If the latter two clearly do not hold, staying with multiple repositories is the safer choice.
4. Adoption Path: Five Stages from Zero to an Operable Setup
Stage 1: Define Package Boundaries
Split packages by “capability units that can be released independently,” not by directory habit. Core capabilities, domain services, and reusable components each become a package. Only one-way dependencies are allowed between packages; circular references are forbidden. Every package must declare a clear public entry point, and internal implementation files are not exposed.
{"name": "@team/core","version": "1.4.0","main": "dist/index.js","types": "dist/index.d.ts"}
Stage 2: Establish Dependency and Version Rules
Declare internal dependencies with the workspace protocol so they always point to local source; the release process replaces them with fixed version numbers when publishing. Semantic versioning is recommended, with a clear convention: bug fixes bump the patch, backward-compatible additions bump the minor, and breaking changes bump the major.
{"dependencies": {"@team/core": "workspace:*"}}
Stage 3: Incremental Scoping of Builds and Tasks
This determines whether a monorepo can scale. The toolchain needs to understand the dependency graph between packages and build only the projects affected by the current change and their downstream dependents.
Change set: packages/core/src/cache.tsAffected set: core → service-a → service-bAction: run build and tests only for core, service-a, service-b
Combined with a content-addressed caching mechanism, unchanged tasks can reuse historical artifacts directly, bringing pipeline duration back from “linear growth with the full codebase” to something proportional to the size of the change.
Stage 4: Gate Design for Continuous Integration
Run type checking, unit tests, and lint checks for the affected scope at the merge request stage; run one full validation on the main branch as a fallback. Set repository size and dependency count thresholds with alerts to prevent uncontrolled addition of sub-packages.
Stage 5: Release and Version Synchronization
The release process must answer two questions: which packages actually changed, and how they become externally visible. A workable approach is to compute the packages with substantive modifications by comparing change sets, publish them in dependency topological order, automatically update the dependency declarations of downstream packages, and generate a changelog at the end. This way, after a business project goes live, the version mapping of core capabilities is always recorded by the repository rather than a manual spreadsheet.
5. Common Anti-Patterns and How to Avoid Them
Stuffing unrelated projects into one repository just to “look unified” results in both builds and permissions getting out of control. The fix is to define admission criteria for the repository.
Sub-packages reference across layers arbitrarily, forming a mesh of dependencies. The fix is to add dependency direction validation to continuous integration so violations fail immediately.
Introducing tooling without writing standards, leaving the directory structure unrecognizable six months later. The fix is to document directory conventions and a review checklist for new packages first, then introduce automation.
Ignoring clone time and disk usage, so the local developer experience keeps degrading. The fix is to adopt repository-side capabilities such as incremental fetching and sparse checkout, and to clean up historical artifacts regularly.
6. Working with a Cloud Engineering Stack
In a cloud environment, a monorepo connects naturally with common capabilities: code hosting and branch protection handle permissions and review gates; a container platform provides a unified build and runtime environment so local and pipeline behavior stay consistent; object storage holds build artifacts and dependency caches; a message queue asynchronously triggers long-running tasks; and logging services plus monitoring alerts establish observable metrics for pipeline duration, failure rate, and cache hit rate. Baidu AI Cloud offers corresponding capabilities in these general categories, and teams can choose a combination based on their own scale without being locked into a single proprietary implementation.
7. Conclusion
The essence of a monorepo is trading a “unified repository boundary” for “shorter feedback loops and a more trustworthy version mapping.” Its benefits concentrate on integration efficiency, atomic commits, and unified governance; its costs concentrate on permission granularity and build scale. The successful adoption order is: first define package boundaries and dependency direction, then add incremental builds and caching, and finally solidify the release and version synchronization process. Following this order, the synchronization problems of multi-project collaboration and version upgrades degrade from routine tasks that require manual coordination into routine steps completed automatically by the pipeline.
