link-workspace-packages

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Link Workspace Packages

链接工作区包

Add dependencies between packages in a monorepo. All package managers support workspaces but with different syntax.
在 monorepo 中为包之间添加依赖。所有包管理器都支持工作区,但语法各不相同。

Detect Package Manager

检测包管理器

Check whether there's a
packageManager
field in the root-level
package.json
.
Alternatively check lockfile in repo root:
  • pnpm-lock.yaml
    → pnpm
  • yarn.lock
    → yarn
  • bun.lock
    /
    bun.lockb
    → bun
  • package-lock.json
    → npm
检查根目录下的
package.json
中是否存在
packageManager
字段。
或者检查仓库根目录的锁文件:
  • pnpm-lock.yaml
    → pnpm
  • yarn.lock
    → yarn
  • bun.lock
    /
    bun.lockb
    → bun
  • package-lock.json
    → npm

Workflow

操作流程

  1. Identify consumer package (the one importing)
  2. Identify provider package(s) (being imported)
  3. Add dependency using package manager's workspace syntax
  4. Verify symlinks created in consumer's
    node_modules/

  1. 确定消费包(即导入其他包的包)
  2. 确定提供包(即被导入的包)
  3. 使用包管理器的工作区语法添加依赖
  4. 验证消费包的
    node_modules/
    中已创建符号链接

pnpm

pnpm

Uses
workspace:
protocol - symlinks only created when explicitly declared.
bash
undefined
使用
workspace:
协议——仅在显式声明时创建符号链接。
bash
undefined

From consumer directory

从消费包目录执行

pnpm add @org/ui --workspace
pnpm add @org/ui --workspace

Or with --filter from anywhere

或者在任意位置使用 --filter 参数

pnpm add @org/ui --filter @org/app --workspace

Result in `package.json`:

```json
{ "dependencies": { "@org/ui": "workspace:*" } }

pnpm add @org/ui --filter @org/app --workspace

`package.json` 中的结果:

```json
{ "dependencies": { "@org/ui": "workspace:*" } }

yarn (v2+/berry)

yarn (v2+/berry)

Also uses
workspace:
protocol.
bash
yarn workspace @org/app add @org/ui
Result in
package.json
:
json
{ "dependencies": { "@org/ui": "workspace:^" } }

同样使用
workspace:
协议。
bash
yarn workspace @org/app add @org/ui
package.json
中的结果:
json
{ "dependencies": { "@org/ui": "workspace:^" } }

npm

npm

No
workspace:
protocol. npm auto-symlinks workspace packages.
bash
npm install @org/ui --workspace @org/app
Result in
package.json
:
json
{ "dependencies": { "@org/ui": "*" } }
npm resolves to local workspace automatically during install.

workspace:
协议。npm 会自动为工作区包创建符号链接。
bash
npm install @org/ui --workspace @org/app
package.json
中的结果:
json
{ "dependencies": { "@org/ui": "*" } }
npm 会在安装过程中自动解析为本地工作区包。

bun

bun

Supports
workspace:
protocol (pnpm-compatible).
bash
cd packages/app && bun add @org/ui
Result in
package.json
:
json
{ "dependencies": { "@org/ui": "workspace:*" } }

支持
workspace:
协议(与 pnpm 兼容)。
bash
cd packages/app && bun add @org/ui
package.json
中的结果:
json
{ "dependencies": { "@org/ui": "workspace:*" } }

Examples

示例

Example 1: pnpm - link ui lib to app
bash
pnpm add @org/ui --filter @org/app --workspace
Example 2: npm - link multiple packages
bash
npm install @org/data-access @org/ui --workspace @org/dashboard
Example 3: Debug "Cannot find module"
  1. Check if dependency is declared in consumer's
    package.json
  2. If not, add it using appropriate command above
  3. Run install (
    pnpm install
    ,
    npm install
    , etc.)
示例1:pnpm - 将UI库链接到应用
bash
pnpm add @org/ui --filter @org/app --workspace
示例2:npm - 链接多个包
bash
npm install @org/data-access @org/ui --workspace @org/dashboard
示例3:调试“Cannot find module”错误
  1. 检查消费包的
    package.json
    中是否已声明该依赖
  2. 如果没有,使用上述对应的命令添加
  3. 执行安装命令(
    pnpm install
    npm install
    等)

Notes

注意事项

  • Symlinks appear in
    <consumer>/node_modules/@org/<package>
  • Hoisting differs by manager:
    • npm/bun: hoist shared deps to root
      node_modules
    • pnpm: no hoisting (strict isolation, prevents phantom deps)
    • yarn berry: uses Plug'n'Play by default (no
      node_modules
      )
  • Root
    package.json
    should have
    "private": true
    to prevent accidental publish
  • 符号链接会出现在
    <consumer>/node_modules/@org/<package>
    目录下
  • 不同管理器的依赖提升机制不同:
    • npm/bun:将共享依赖提升到根目录的
      node_modules
    • pnpm:不进行依赖提升(严格隔离,避免幽灵依赖)
    • yarn berry:默认使用 Plug'n'Play 机制(无
      node_modules
      目录)
  • 根目录的
    package.json
    应设置
    "private": true
    ,以防止意外发布