pnpm-workspace
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesepnpm Workspace
pnpm Workspace
Overview
概述
pnpm workspaces provide built-in monorepo support through , the protocol for local package linking, and powerful filtering to run commands across specific packages. Catalogs enforce consistent dependency versions across all workspace packages.
pnpm-workspace.yamlworkspace:When to use: Multi-package repositories, shared libraries with consuming apps, consistent dependency management across packages, running commands on subsets of packages.
When NOT to use: Single-package projects, projects already using npm/yarn workspaces (migration required), projects that need floating dependency versions per package.
pnpm workspaces通过、用于本地包链接的协议,以及强大的过滤功能为monorepo提供内置支持,可跨特定包运行命令。Catalogs可确保所有工作区包的依赖版本保持一致。
pnpm-workspace.yamlworkspace:适用场景: 多包仓库、包含消费应用的共享库、跨包的一致依赖管理、在部分包子集上运行命令。
不适用场景: 单包项目、已使用npm/yarn workspaces的项目(需要迁移)、每个包需要浮动依赖版本的项目。
Quick Reference
快速参考
| Pattern | API / Config | Key Points |
|---|---|---|
| Define workspace | | Globs match directories containing |
| Link local package | | Always resolves to local workspace package |
| Link with version range | | Fails install if local version does not satisfy range |
| Default catalog | | Single source of truth for dependency versions |
| Named catalog | | Multiple version sets (e.g., |
| Use catalog in package | | Resolved to actual version on |
| Filter by name | | Exact name or glob pattern ( |
| Filter with dependencies | | Package and all its dependencies |
| Filter with dependents | | Package and all packages that depend on it |
| Filter by directory | | All packages under a directory path |
| Filter by git changes | | Packages changed since a commit or branch |
| Exclude from filter | | Remove matching packages from selection |
| Run script in package | | Runs script only in matched packages |
| Recursive run | | Runs script in all workspace packages |
| Install all | | Single lockfile for entire workspace |
| Publish workspace pkg | | Replaces |
| Inject workspace deps | | Hard-links instead of symlinks; required for deploy |
| Script security (v10+) | | Lifecycle scripts blocked by default; opt-in per dep |
| 模式 | API/配置 | 关键点 |
|---|---|---|
| 定义工作区 | 带有 | 通配符匹配包含 |
| 链接本地包 | | 始终解析为本地工作区包 |
| 带版本范围的链接 | | 若本地版本不满足范围,安装会失败 |
| 默认Catalog | | 依赖版本的单一可信来源 |
| 命名Catalog | 带有命名分组的 | 多版本集合(例如 |
| 在包中使用Catalog | | 在 |
| 按名称过滤 | | 精确名称或通配符模式( |
| 按依赖过滤 | | 包及其所有依赖 |
| 按被依赖过滤 | | 包及其所有依赖它的包 |
| 按目录过滤 | | 指定目录下的所有包 |
| 按Git变更过滤 | | 自某次提交或分支以来有变更的包 |
| 从过滤结果中排除 | | 从选中的包中移除匹配的包 |
| 在包中运行脚本 | | 仅在匹配的包中运行脚本 |
| 递归运行 | | 在所有工作区包中运行脚本 |
| 安装所有依赖 | | 整个工作区使用单一锁文件 |
| 发布工作区包 | | 将 |
| 注入工作区依赖 | | 使用硬链接而非符号链接;部署时必需 |
| 脚本安全性(v10+) | | 默认阻止生命周期脚本;需为每个依赖手动开启 |
Common Mistakes
常见错误
| Mistake | Correct Pattern |
|---|---|
Using | pnpm automatically replaces |
Forgetting to list directory in | Every package directory must match a glob in |
Using | Always use |
| Hardcoding versions duplicated across packages | Use |
Running | Run from workspace root; use |
Expecting | |
Not escaping | Use |
Using | |
| Lifecycle scripts failing after pnpm 10 upgrade | pnpm 10 blocks scripts by default; add deps to |
Using | Set |
| 错误操作 | 正确做法 |
|---|---|
使用 | pnpm会在发布时自动将 |
忘记在 | 每个包目录必须匹配 |
在工作区包中使用 | 始终从工作区根目录运行 |
| 在多个包中硬编码重复的版本 | 使用 |
在子包内运行 | 从工作区根目录运行;使用 |
期望 | |
在zsh中使用排除过滤时未转义 | 使用 |
对非工作区依赖使用 | |
| 升级到pnpm 10后生命周期脚本执行失败 | pnpm 10默认阻止脚本;需在配置中将依赖添加到 |
未设置 | 在 |
Delegation
任务委托
- Workspace scaffolding: Use agent to discover existing package structure
Explore - Dependency auditing: Use agent to check version consistency across packages
Task
If theskill is available, delegate build orchestration, task caching, and CI optimization to it. If theturboreposkill is available, delegate versioning, changelog generation, and npm publishing to it.changesets
- 工作区脚手架搭建:使用Explore Agent探索现有包结构
- 依赖审计:使用Task Agent检查跨包的版本一致性
若Skill可用,将构建编排、任务缓存和CI优化委托给它。 若turborepoSkill可用,将版本管理、变更日志生成和npm发布委托给它。changesets
References
参考资料
- Workspace setup, workspace protocol, and catalogs
- Filtering packages for targeted commands
- Shared configuration across workspace packages
- Monorepo integration: pnpm + Turborepo + Changesets pipeline
- 工作区设置、workspace协议和Catalogs
- 针对目标命令过滤包
- 跨工作区包共享配置
- Monorepo集成:pnpm + Turborepo + Changesets流水线