Git Advanced: Monorepo, Submodules & Workflows · 课时

单体仓库中的依赖管理与版本控制

了解如何处理共享依赖、内部软件包版本控制,以及大型单体仓库中的统一工具链,从而保持构建可复现且无冲突。

第 4 / 4 课13 个步骤

单体仓库中的依赖管理与版本控制 是 CoddyKit 上的免费 Git Advanced: Monorepo, Submodules & Workflows 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Git Advanced: Monorepo, Submodules & Workflows 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Git Advanced: Monorepo, Submodules & Workflows 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Why Dependency Management Matters

In a monorepo, dozens of projects share a single tree. Without discipline, you end up with three different versions of the same library coexisting, bloating bundles and causing subtle bugs.

Centralized dependency management gives you one source of truth for versions, making upgrades and security patches a single coordinated change instead of dozens of scattered ones.

Single-Version Policy

A common monorepo strategy is the single-version policy: every project depends on exactly one version of each third-party library.

  • Eliminates version skew between teams
  • Forces upgrades to be evaluated holistically
  • Simplifies the dependency graph for build tools

The tradeoff: an upgrade can require changes across many projects at once.

Workspaces in Practice

Package managers like npm, yarn, and pnpm support workspaces: a top-level config that hoists shared dependencies and links internal packages locally.

{
  "name": "my-monorepo",
  "private": true,
  "workspaces": [
    "packages/*",
    "apps/*"
  ]
}

Linking Internal Packages

Internal packages reference each other by name, not by relative path. The package manager symlinks them so a change in one is immediately visible in another.

{
  "name": "@acme/web-app",
  "dependencies": {
    "@acme/ui": "workspace:*",
    "@acme/utils": "workspace:*"
  }
}

Pinning vs. Ranges

Should you pin exact versions or allow ranges?

  • Exact pins (1.4.2) give reproducible builds but require manual bumps.
  • Ranges (^1.4.0) auto-pick patches but risk drift.

In monorepos, prefer exact pins plus a lockfile so every machine resolves identical trees.

The Lockfile Is Sacred

The lockfile (package-lock.json, yarn.lock, pnpm-lock.yaml) records the exact resolved tree. Commit it, review changes to it, and never hand-edit it.

A surprising lockfile diff during code review is often the first sign of an unintended dependency change.

Catalogs and Centralized Versions

Modern tools support catalogs: a central list of versions referenced by name across packages, so you bump one place instead of many.

# pnpm-workspace.yaml
catalog:
  react: 18.3.1
  typescript: 5.4.5

# in a package.json
# "react": "catalog:"

Versioning Internal Releases

When internal packages are published externally, you need a release process. Tools like Changesets let contributors declare the impact of a change, then compute the next semver bump automatically.

# .changeset/brave-lions-jump.md
---
"@acme/ui": minor
"@acme/utils": patch
---
Add new Button variant and fix date helper.

Detecting Drift

Drift happens when versions silently diverge. Add a CI check that fails the build if duplicate versions of a critical dependency appear.

# Fail if more than one version of react is resolved
npm ls react --all | grep -c 'react@' 

Coordinated Upgrades

Upgrade a shared dependency in one branch, run the affected projects' tests, and merge atomically. This avoids the half-migrated state where some apps are on the new version and some are not.

Automation bots can open these upgrade PRs and let CI prove safety before merge.

Auditing for Security

Because versions are centralized, a single audit run covers the whole repo. Wire it into CI so vulnerable transitive dependencies block merges.

pnpm audit --audit-level=high

Quick Check

Test your understanding of monorepo dependency management.

Recap

You learned how monorepos keep dependencies sane: a single-version policy, workspaces for linking internal packages, catalogs for centralized versions, a sacred lockfile, drift detection in CI, and coordinated, auditable upgrades.

Disciplined dependency management is what keeps a large monorepo reproducible and trustworthy.

免费开始

用 AI 导师学习 Git Advanced: Monorepo, Submodules & Workflows — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
12
课程
48

常见问题解答

「单体仓库中的依赖管理与版本控制」课时是免费的吗?

是的 — 「单体仓库中的依赖管理与版本控制」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Git Advanced: Monorepo, Submodules & Workflows 课程的其余内容,请升级到 CoddyKit PRO。 Git Advanced: Monorepo, Submodules & Workflows 课程共包含 4 节课。

「单体仓库中的依赖管理与版本控制」这节课中我会学到什么?

了解如何处理共享依赖、内部软件包版本控制,以及大型单体仓库中的统一工具链,从而保持构建可复现且无冲突。 你通过在浏览器中直接运行的动手代码来练习 Git Advanced: Monorepo, Submodules & Workflows,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Git Advanced: Monorepo, Submodules & Workflows 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Git Advanced: Monorepo, Submodules & Workflows 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「单体仓库中的依赖管理与版本控制」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Git Advanced: Monorepo, Submodules & Workflows 课中编写并运行代码吗?

能。每节 Git Advanced: Monorepo, Submodules & Workflows 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 代码所有权与访问控制
  2. 优化构建与测试性能
  3. 单体仓库迁移策略
  4. 单体仓库中的依赖管理与版本控制
← 返回 Git Advanced: Monorepo, Submodules & Workflows