JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeTopicstoolingMonorepos & Project Architecture
Next
tooling
advanced
15 min read

Monorepos & Project Architecture

architecture
monorepo
nx
project-structure
turborepo
workspaces

Monorepo architecture uses a single repository for multiple packages and applications, leveraging tools like Turborepo, Nx, and pnpm workspaces to manage shared dependencies, coordinated builds, and consistent tooling across projects.

Key Points

1Workspace Package Linking

pnpm, Yarn, and npm workspaces let local packages reference each other via workspace protocols without publishing to a registry, enabling atomic cross-package changes.

2Build Orchestration

Turborepo and Nx analyze the dependency graph to run tasks in topological order, skipping unchanged packages and caching outputs for dramatic CI speed improvements.

3Remote Caching

Build outputs are cached by input hash and shared across developers and CI, so identical work is never repeated — Turborepo can reduce CI times by 40-80%.

4Monorepo vs Monolith

A monorepo is a repository strategy containing independent packages with clear boundaries; a monolith is a tightly coupled single application. They are fundamentally different concepts.

What You'll Learn

  • Explain the benefits and tradeoffs of monorepo vs polyrepo architecture
  • Set up a monorepo with pnpm workspaces and Turborepo
  • Describe how remote caching and topological task scheduling work
  • Design a monorepo structure that separates apps from shared packages

Deep Dive

A monorepo is a single repository containing multiple distinct projects, packages, or applications that may or may not be related. This is in contrast to polyrepo architecture, where each project lives in its own repository. Companies like Google, Meta, and Microsoft use monorepos at massive scale, and the JavaScript ecosystem has embraced this pattern for managing complex frontend applications and shared libraries.

Why Monorepos?

The primary benefits are code sharing, atomic changes, and unified tooling. In a polyrepo setup, sharing code between projects requires publishing packages to a registry, managing versions, and coordinating updates across repositories. In a monorepo, packages import from each other directly using workspace protocols (workspace:*), and a single pull request can update a shared library and all its consumers simultaneously. This eliminates version drift and "dependency hell" that plague polyrepo setups.

Workspace Package Managers

pnpm workspaces, Yarn workspaces, and npm workspaces all provide the foundational capability: linking local packages together so they can reference each other without publishing. pnpm is particularly well-suited for monorepos because its content-addressable storage and strict dependency isolation prevent phantom dependencies (packages accidentally available because a sibling installed them). The pnpm-workspace.yaml file defines which directories contain packages.

Build Orchestration with Turborepo

Turborepo solves the key challenge of monorepo builds: dependency-aware task scheduling. It reads the dependency graph from your workspace configuration and runs tasks in the correct topological order. If package B depends on package A, Turborepo ensures A builds before B. It also provides remote caching — if a teammate already built a package with identical inputs, Turborepo downloads the cached output instead of rebuilding. This can reduce CI times by 40-80% in practice.

Nx as an Alternative

Nx offers similar build orchestration with additional features like affected-command analysis (only running tasks on packages changed since the last commit), computation caching, and a rich plugin ecosystem for specific frameworks. Nx tends to be more opinionated and feature-rich, while Turborepo focuses on being lightweight and easy to adopt incrementally.

Typical Monorepo Structure

A well-organized monorepo separates deployable applications (apps/) from reusable libraries (packages/). Shared configurations (TypeScript, ESLint, Prettier) live in dedicated packages and are extended by consumers. This structure enforces clear boundaries: a UI component library does not depend on a specific application, making it reusable across all apps.

Common Interview Questions

Interviewers often ask about tradeoffs. Monorepos can suffer from slow CI without proper caching, complex tooling setup, and repository size growth. They also ask about dependency management — how workspace protocols work, how you ensure packages use compatible versions of shared dependencies, and how you handle publishing individual packages from a monorepo to npm.

Key Interview Distinction

A monorepo is not a monolith. A monolith is a single application with tightly coupled components. A monorepo is a repository strategy that can contain many independent, well-bounded packages. The packages can be deployed independently and have their own release cycles.

Fun Fact

Google's monorepo contains over 2 billion lines of code across 86 terabytes and is accessed by 25,000+ developers daily. They built a custom version control system called Piper because Git cannot handle a repository that large.

Learn These First

Package Managers & Dependency Management

beginner

Continue Learning

Build Performance Optimization

advanced

CI/CD Pipelines for JavaScript Projects

intermediate

Practice What You Learned

What is a monorepo and how do you set one up?
senior
architecture
A monorepo contains multiple packages/projects in one repository. Use tools like Turborepo, Nx, or pnpm workspaces for dependency management, shared configs, and efficient builds. Benefits include code sharing, atomic changes, and unified tooling.
Next
JavaScript Bundlers & Build Tools
Next