|
| 1 | +--- |
| 2 | +name: link-workspace-packages |
| 3 | +description: 'Link workspace packages in monorepos (npm, yarn, pnpm, bun). USE WHEN: (1) you just created or generated new packages and need to wire up their dependencies, (2) user imports from a sibling package and needs to add it as a dependency, (3) you get resolution errors for workspace packages (@org/*) like "cannot find module", "failed to resolve import", "TS2307", or "cannot resolve". DO NOT patch around with tsconfig paths or manual package.json edits - use the package manager''s workspace commands to fix actual linking.' |
| 4 | +--- |
| 5 | + |
| 6 | +# Link Workspace Packages |
| 7 | + |
| 8 | +Add dependencies between packages in a monorepo. All package managers support workspaces but with different syntax. |
| 9 | + |
| 10 | +## Detect Package Manager |
| 11 | + |
| 12 | +Check whether there's a `packageManager` field in the root-level `package.json`. |
| 13 | + |
| 14 | +Alternatively check lockfile in repo root: |
| 15 | + |
| 16 | +- `pnpm-lock.yaml` → pnpm |
| 17 | +- `yarn.lock` → yarn |
| 18 | +- `bun.lock` / `bun.lockb` → bun |
| 19 | +- `package-lock.json` → npm |
| 20 | + |
| 21 | +## Workflow |
| 22 | + |
| 23 | +1. Identify consumer package (the one importing) |
| 24 | +2. Identify provider package(s) (being imported) |
| 25 | +3. Add dependency using package manager's workspace syntax |
| 26 | +4. Verify symlinks created in consumer's `node_modules/` |
| 27 | + |
| 28 | +--- |
| 29 | + |
| 30 | +## pnpm |
| 31 | + |
| 32 | +Uses `workspace:` protocol - symlinks only created when explicitly declared. |
| 33 | + |
| 34 | +```bash |
| 35 | +# From consumer directory |
| 36 | +pnpm add @org/ui --workspace |
| 37 | + |
| 38 | +# Or with --filter from anywhere |
| 39 | +pnpm add @org/ui --filter @org/app --workspace |
| 40 | +``` |
| 41 | + |
| 42 | +Result in `package.json`: |
| 43 | + |
| 44 | +```json |
| 45 | +{ "dependencies": { "@org/ui": "workspace:*" } } |
| 46 | +``` |
| 47 | + |
| 48 | +--- |
| 49 | + |
| 50 | +## yarn (v2+/berry) |
| 51 | + |
| 52 | +Also uses `workspace:` protocol. |
| 53 | + |
| 54 | +```bash |
| 55 | +yarn workspace @org/app add @org/ui |
| 56 | +``` |
| 57 | + |
| 58 | +Result in `package.json`: |
| 59 | + |
| 60 | +```json |
| 61 | +{ "dependencies": { "@org/ui": "workspace:^" } } |
| 62 | +``` |
| 63 | + |
| 64 | +--- |
| 65 | + |
| 66 | +## npm |
| 67 | + |
| 68 | +No `workspace:` protocol. npm auto-symlinks workspace packages. |
| 69 | + |
| 70 | +```bash |
| 71 | +npm install @org/ui --workspace @org/app |
| 72 | +``` |
| 73 | + |
| 74 | +Result in `package.json`: |
| 75 | + |
| 76 | +```json |
| 77 | +{ "dependencies": { "@org/ui": "*" } } |
| 78 | +``` |
| 79 | + |
| 80 | +npm resolves to local workspace automatically during install. |
| 81 | + |
| 82 | +--- |
| 83 | + |
| 84 | +## bun |
| 85 | + |
| 86 | +Supports `workspace:` protocol (pnpm-compatible). |
| 87 | + |
| 88 | +```bash |
| 89 | +cd packages/app && bun add @org/ui |
| 90 | +``` |
| 91 | + |
| 92 | +Result in `package.json`: |
| 93 | + |
| 94 | +```json |
| 95 | +{ "dependencies": { "@org/ui": "workspace:*" } } |
| 96 | +``` |
| 97 | + |
| 98 | +--- |
| 99 | + |
| 100 | +## Examples |
| 101 | + |
| 102 | +**Example 1: pnpm - link ui lib to app** |
| 103 | + |
| 104 | +```bash |
| 105 | +pnpm add @org/ui --filter @org/app --workspace |
| 106 | +``` |
| 107 | + |
| 108 | +**Example 2: npm - link multiple packages** |
| 109 | + |
| 110 | +```bash |
| 111 | +npm install @org/data-access @org/ui --workspace @org/dashboard |
| 112 | +``` |
| 113 | + |
| 114 | +**Example 3: Debug "Cannot find module"** |
| 115 | + |
| 116 | +1. Check if dependency is declared in consumer's `package.json` |
| 117 | +2. If not, add it using appropriate command above |
| 118 | +3. Run install (`pnpm install`, `npm install`, etc.) |
| 119 | + |
| 120 | +## Notes |
| 121 | + |
| 122 | +- Symlinks appear in `<consumer>/node_modules/@org/<package>` |
| 123 | +- **Hoisting differs by manager:** |
| 124 | + - npm/bun: hoist shared deps to root `node_modules` |
| 125 | + - pnpm: no hoisting (strict isolation, prevents phantom deps) |
| 126 | + - yarn berry: uses Plug'n'Play by default (no `node_modules`) |
| 127 | +- Root `package.json` should have `"private": true` to prevent accidental publish |
0 commit comments