# Monorepos & Workspaces (npm, pnpm & Turborepo)

> Master monorepo package management using npm workspaces, pnpm workspaces, and Turborepo task pipeline caching for fast, modular multi-package projects.

Subject: Package Managers | Lesson 19 of 24 | 6 min read
Interactive lesson: https://codewithanimation.in/package-managers/npm-workspaces-monorepos
Previous: [Hotfixing Dependencies (patch-package & pnpm patch)](https://codewithanimation.in/package-managers/patch-package-and-pnpm-patch.md) | Next: [Publishing Packages & Private Registries (npm publish & .npmrc)](https://codewithanimation.in/package-managers/publishing-and-private-registries.md)

## What are Workspaces and Monorepos?

A monorepo (monolithic repository) is a version control pattern where multiple projects, applications, and shared libraries reside inside a single repository. Workspaces are package manager features native to npm, pnpm, and Yarn that link these internal packages together automatically.

Instead of publishing shared UI components or utility functions to an external npm registry during development, workspaces create local symlinks inside `node_modules`. This allows applications like `apps/web` to import internal packages like `@myorg/ui` instantly with live code updates.

## Why manage projects with monorepos and build orchestrators?

Separate repositories force developers to publish, version, and reinstall internal packages every time a shared component changes. Monorepos unify your codebase so changes across multiple applications and libraries can be made in a single atomic git commit.

However, running builds and tests across dozens of packages sequentially is slow. Build orchestrators like Turborepo and Nx analyze your monorepo dependency graph to execute tasks in parallel, skipping unneeded work via intelligent local and remote computation caching.

## How Workspaces Link Packages and Turborepo Orchestrates Pipelines

In npm workspaces, the root `package.json` defines a `workspaces` array listing package directories such as `apps/*` and `packages/*`. Running `npm install` automatically hoists shared dependencies to root `node_modules` and symlinks internal packages.

In pnpm workspaces, packages are defined in `pnpm-workspace.yaml`, and internal dependencies use the explicit `workspace:*` protocol inside `package.json`. Turborepo then uses a `turbo.json` file where `dependsOn: ["^build"]` instructs the task runner to build shared internal packages before compiling dependent applications.

## Common mistake: Circular dependencies and hoisting conflicts break monorepo builds

A common mistake in monorepos is creating circular dependencies where Package A imports Package B, which in turn imports Package A. This prevents task orchestrators from determining execution order and causes runtime undefined exports.

Another pitfall is version mismatch hoisting conflicts. If two apps require conflicting major versions of a shared library, package managers nested-hoist dependencies into separate sub-folders, which can break singleton runtime patterns like React Context.

## Tip: Use workspace:* protocol, turbo.json task pipelines, and remote caching

Always use explicit workspace protocols like `"@myorg/ui": "workspace:*"` in pnpm to prevent accidental registry downloads. Configure Turborepo inputs and outputs strictly in `turbo.json` to achieve instant `FULL TURBO` cache hits.

Enable remote caching in CI/CD pipelines so team members share pre-compiled build artifacts, eliminating redundant compilation time across your organization.

## Try it: Interactive Monorepo Workspace & Turborepo Pipeline Simulator

Explore workspace symlinks, simulate topological build execution graphs, and test Turborepo computation caching in real time.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/package-managers/npm-workspaces-monorepos

## Check yourself

### Question 1

What does the root npm workspaces declaration identify?

- A. Only remote registry URLs
- B. A list of Git branches to merge
- C. Local package directories managed together by the project
- D. The files included in a package tarball

Correct answer: C

npm uses the workspace configuration to recognize local packages and link them during installation. Each package still has its own manifest and requirements.

- Why not A: Workspaces identify local package directories.
- Why not B: Workspace configuration does not merge branches.
- Why not D: The files field and packaging rules control that payload.

### Question 2

Which distinction between workspaces and a task runner is correct?

- A. Workspaces always compile every app automatically
- B. A task runner replaces all package manifests
- C. A monorepo requires publishing every internal package first
- D. The manager links packages; the task runner coordinates their tasks

Correct answer: D

Workspaces provide package-management relationships. A tool such as Turborepo can schedule builds and cache declared outputs using the task graph.

- Why not A: Linking packages does not by itself run every build.
- Why not B: Task runners still rely on package/task definitions.
- Why not C: Workspaces can link internal packages locally.

### Question 3

In a Turborepo build task, what does dependsOn: ["^build"] express?

- A. Build dependency packages before building their dependent package
- B. Build only the current package twice
- C. Always skip compilation because the task is cached
- D. Publish every dependency to a registry

Correct answer: A

The caret refers to tasks in package dependencies. Correct output/cache configuration is separate from establishing this task order.

- Why not B: The caret targets dependency-package tasks.
- Why not C: A dependency declaration is not a guaranteed cache hit.
- Why not D: Build-task ordering does not authorize publication.

## Remember this

- Workspaces link local internal packages using symlinks in npm or workspace:* protocols in pnpm.
- Turborepo uses turbo.json with dependsOn: ["^build"] to establish topological dependency build order.
- Turborepo can reuse cached task outputs when the declared inputs/configuration match; configure outputs correctly and do not assume every build is a cache hit.
- Avoid circular dependencies between internal packages to prevent broken build pipelines and undefined exports.
