Workspaces in npm, Yarn, and pnpm
What you'll learn: How npm, Yarn, and pnpm each implement workspaces to link and manage multiple packages within a single repository.
The Core Concept
Workspaces are a built-in feature in modern package managers that let you define multiple packages in one repo and automatically link them together. Instead of publishing each package separately and installing them as external dependencies, workspaces create local symlinks so packages can reference each other as if they were published—without leaving your monorepo.
Think of workspaces like a shared office building where each department (package) has its own space, but they all share common resources (dependencies) and can easily communicate without leaving the building.
How Each Package Manager Implements Workspaces
npm (v7+): Define workspaces in the root package.json using the "workspaces" field with glob patterns:
{
"workspaces": ["packages/*"]
}
npm automatically hoists shared dependencies to the root node_modules and creates symlinks between workspace packages.
Yarn (Classic and Berry): Very similar syntax to npm, also using the "workspaces" field. Yarn was actually the first to introduce workspaces, which inspired npm's implementation.
pnpm: Uses a pnpm-workspace.yaml file instead of package.json:
packages:
- 'packages/*'
pnpm's strict symlink structure means workspace packages are linked but still maintain isolated dependency trees.
Key Differences
All three handle dependency hoisting differently. npm and Yarn Classic aggressively hoist shared dependencies, while pnpm keeps stricter isolation. Commands like npm install run across all workspaces, and you can target specific packages with flags like --workspace=package-name.
Key Takeaway: Workspaces eliminate manual linking between monorepo packages—npm, Yarn, and pnpm each provide native workspace support with similar APIs but different internal dependency management strategies.