npm Workspaces
Purpose
Set up an npm-based monorepo so internal packages link and shared code works, with reproducible installs — the workspace foundation when monorepo-selection chose npm (already standardized, simpler than adding pnpm).
When to Use
- After
monorepo-selection chose npm workspaces.
- Not for pnpm repos (
pnpm-workspaces) or single repos.
Inputs
- Package/app inventory and shared packages.
- Node/npm version policy; whether Turborepo orchestrates (
turborepo-foundation).
Discovery Questions
- What is the workspace layout (
apps/*, packages/*) and which packages are shared?
- Which internal packages depend on which?
- How are Node/npm versions pinned and the lockfile enforced in CI?
Responsibilities
- Define the
workspaces field (package globs) and an apps/ vs packages/ layout.
- Link internal packages via workspaces so cross-package imports resolve to local source; use
*/workspace-range dependencies for internal packages.
- Note npm's hoisting behavior: the shared
node_modules can expose phantom dependencies (packages resolvable but not declared) — still declare every dependency each package imports, so it survives extraction/publish.
- Decide shared-package versioning: lockstep vs independent publish; record it.
- Enforce reproducibility: committed
package-lock.json, npm ci (not npm install) in CI, pinned npm/Node via packageManager/engines + corepack.
- Coordinate with
docker-foundation (cached npm ci layers) and ci-cd.
Required Workflow
- Define workspace globs + layout.
- Wire internal package linking.
- Enforce explicit dependency declarations (guard against phantom deps).
- Decide shared-package versioning.
- Enforce
npm ci + pinned versions in CI.
Decision Rules
- Every imported package is declared in that package's
package.json, regardless of hoisting — phantom deps break on publish/extraction.
npm ci against a committed lockfile in CI; npm install in CI drifts.
- Pin npm/Node so installs match across environments.
- If strict isolation or install efficiency becomes a pain point, that's a signal to revisit
monorepo-selection (pnpm) — record it rather than fighting hoisting.
Rules
- Lockfile committed;
npm ci in CI.
- Explicit dependency declarations per package.
- Node/npm versions pinned.
Anti-Patterns
- Relying on hoisted phantom dependencies.
npm install in CI instead of npm ci.
- Uncommitted/unpinned lockfile → non-reproducible installs.
- Flat package layout with unclear boundaries.
- Registry versions for local internal packages.
Validation Checklist
Definition of Done
An npm-workspace setup with a clear layout, linked internal packages, explicit per-package dependencies, a recorded versioning policy, and reproducible npm ci installs with pinned versions.
Related Skills
monorepo-selection, pnpm-workspaces, turborepo-foundation, docker-foundation, ci-cd, ../../security/dependency-security.
Related Knowledge
../../../knowledge/ (package graph, version policy).
Related References
../../../references/devops/ (workspace patterns, when populated).
Context Loading Guidance
- Requires: package inventory, internal dep graph, version policy.
- Does not require: app feature code, unrelated infra.
- May load:
turborepo-foundation, ci-cd.
- Stop when: layout, linking, dependency discipline, versioning, and lockfile are set.
Token Efficiency Guidance
The layout + internal-dep list is the artifact; keep policy to declarations and versioning.
1---2name: npm-workspaces3description: Use to plan an npm-workspaces monorepo — workspace layout, internal package linking, shared-dependency and versioning policy, and lockfile/CI discipline — when npm is the standardized package manager and simplicity is preferred over pnpm.4---56# npm Workspaces78## Purpose910Set up an npm-based monorepo so internal packages link and shared code works, with reproducible installs — the workspace foundation when `monorepo-selection` chose npm (already standardized, simpler than adding pnpm).1112## When to Use1314- After `monorepo-selection` chose npm workspaces.15- **Not** for pnpm repos (`pnpm-workspaces`) or single repos.1617## Inputs1819- Package/app inventory and shared packages.20- Node/npm version policy; whether Turborepo orchestrates (`turborepo-foundation`).2122## Discovery Questions2324- What is the workspace layout (`apps/*`, `packages/*`) and which packages are shared?25- Which internal packages depend on which?26- How are Node/npm versions pinned and the lockfile enforced in CI?2728## Responsibilities2930- Define the `workspaces` field (package globs) and an `apps/` vs `packages/` layout.31- Link internal packages via workspaces so cross-package imports resolve to local source; use `*`/workspace-range dependencies for internal packages.32- Note npm's **hoisting** behavior: the shared `node_modules` can expose **phantom dependencies** (packages resolvable but not declared) — still declare every dependency each package imports, so it survives extraction/publish.33- Decide **shared-package versioning**: lockstep vs independent publish; record it.34- Enforce **reproducibility**: committed `package-lock.json`, `npm ci` (not `npm install`) in CI, pinned npm/Node via `packageManager`/engines + corepack.35- Coordinate with `docker-foundation` (cached `npm ci` layers) and `ci-cd`.3637## Required Workflow38391. Define workspace globs + layout.402. Wire internal package linking.413. Enforce explicit dependency declarations (guard against phantom deps).424. Decide shared-package versioning.435. Enforce `npm ci` + pinned versions in CI.4445## Decision Rules4647- Every imported package is declared in that package's `package.json`, regardless of hoisting — phantom deps break on publish/extraction.48- `npm ci` against a committed lockfile in CI; `npm install` in CI drifts.49- Pin npm/Node so installs match across environments.50- If strict isolation or install efficiency becomes a pain point, that's a signal to revisit `monorepo-selection` (pnpm) — record it rather than fighting hoisting.5152## Rules5354- Lockfile committed; `npm ci` in CI.55- Explicit dependency declarations per package.56- Node/npm versions pinned.5758## Anti-Patterns5960- Relying on hoisted phantom dependencies.61- `npm install` in CI instead of `npm ci`.62- Uncommitted/unpinned lockfile → non-reproducible installs.63- Flat package layout with unclear boundaries.64- Registry versions for local internal packages.6566## Validation Checklist6768- [ ] Workspace globs + layout defined.69- [ ] Internal linking wired.70- [ ] All imports explicitly declared per package.71- [ ] Shared-package versioning decided.72- [ ] `npm ci` + pinned versions in CI.7374## Definition of Done7576An npm-workspace setup with a clear layout, linked internal packages, explicit per-package dependencies, a recorded versioning policy, and reproducible `npm ci` installs with pinned versions.7778## Related Skills7980`monorepo-selection`, `pnpm-workspaces`, `turborepo-foundation`, `docker-foundation`, `ci-cd`, `../../security/dependency-security`.8182## Related Knowledge8384`../../../knowledge/` (package graph, version policy).8586## Related References8788`../../../references/devops/` (workspace patterns, when populated).8990## Context Loading Guidance9192- **Requires:** package inventory, internal dep graph, version policy.93- **Does not require:** app feature code, unrelated infra.94- **May load:** `turborepo-foundation`, `ci-cd`.95- **Stop when:** layout, linking, dependency discipline, versioning, and lockfile are set.9697## Token Efficiency Guidance9899The layout + internal-dep list is the artifact; keep policy to declarations and versioning.