TypeScript Monorepo Setup with Project References: A Practical Workflow
typescriptmonorepoproject-referencesbuild-toolsworkspaces

TypeScript Monorepo Setup with Project References: A Practical Workflow

TTypeScript.page Editorial Team
2026-11-29
6 min read

Configure a TypeScript monorepo with project references, composite projects, declaration outputs, build checks, and practical troubleshooting steps.

A TypeScript monorepo becomes easier to build and maintain when each package is an explicit project with its own configuration, outputs, and dependency boundaries. This guide provides a reusable project-references workflow, starter directory layout, tsconfig templates, build-order checks, and fixes for common declaration and incremental-build errors.

Overview

TypeScript project references let one TypeScript project depend on another through a declared reference. Instead of treating the entire repository as one large compilation, you divide it into smaller composite projects and let the TypeScript build mode understand their relationships.

A practical monorepo often contains a shared package and one or more applications:

repo/
  apps/
    web/
      src/
      tsconfig.json
      package.json
  packages/
    shared/
      src/
      tsconfig.json
      package.json
  tsconfig.json
  package.json

The root configuration coordinates the projects; it does not usually compile application source itself. Each referenced package should emit JavaScript and declaration files, or otherwise provide the outputs required by the consuming project. A package manager workspace can link packages for installation, but workspace linking alone does not configure TypeScript project references. You need both layers: package-manager dependency declarations and TypeScript references.

For a detailed repository-wide quality baseline, pair this workflow with a phased TypeScript strict mode migration checklist. If the monorepo is being created during a JavaScript migration, establish file ownership and build checks before moving every package at once.

Checklist by scenario

1. Starting a small monorepo

  1. Create one tsconfig.json inside each independently buildable package or application.
  2. Set composite to true in every project included by a reference.
  3. Give each project a stable rootDir and an output directory outside the source tree, such as dist.
  4. Define references in the consuming project and add the corresponding package dependency in package.json.
  5. Add a root solution configuration containing references but no application source files.

A minimal root solution configuration can look like this:

{
  "files": [],
  "references": [
    { "path": "./packages/shared" },
    { "path": "./apps/web" }
  ]
}

The shared package configuration might be:

{
  "compilerOptions": {
    "target": "ES2022",
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "strict": true,
    "composite": true,
    "declaration": true,
    "declarationMap": true,
    "rootDir": "src",
    "outDir": "dist",
    "tsBuildInfoFile": "./dist/tsconfig.tsbuildinfo"
  },
  "include": ["src"]
}

The application can reference the shared project:

{
  "extends": "../../tsconfig.base.json",
  "compilerOptions": {
    "composite": true,
    "rootDir": "src",
    "outDir": "dist"
  },
  "include": ["src"],
  "references": [
    { "path": "../../packages/shared" }
  ]
}

Many teams keep reusable options in a root tsconfig.base.json, while retaining include, rootDir, outDir, and project-specific references in each package.

2. Building the dependency graph

  1. Draw the dependency direction before adding references: applications may depend on libraries, but libraries should not accidentally depend back on applications.
  2. Make every importable workspace package a declared dependency of its consumer.
  3. Add a TypeScript reference for every local TypeScript project that must be built first.
  4. Run tsc -b from the repository root and inspect the order in which projects are selected.
  5. Run tsc -b --clean when testing a clean build rather than relying only on existing output.

Build mode uses the reference graph and incremental metadata to determine which projects need work. The exact command used by a workspace task runner may differ, but the underlying graph should remain understandable without that tool.

3. Adding a new package

  1. Copy the package-level configuration from a known-good package, then change its name, paths, and entry points.
  2. Confirm the package emits declarations if other TypeScript projects consume its types.
  3. Add the package to the workspace configuration used by your package manager.
  4. Add a package dependency and a TypeScript reference in each consumer.
  5. Add the new project to the root solution configuration if it should participate in the standard build.
  6. Run a clean build and a consumer type-check before opening a pull request.

What to double-check

Composite requirements: A referenced project generally needs composite: true. Composite projects impose stricter file accounting, so an incomplete include pattern or an unexpected source file can produce an error. Keep source directories deliberate and check whether tests, generated files, and scripts belong in the project.

Declaration output: Consumers should resolve the built package declarations rather than silently reaching into another package's source directory. Check declaration, outDir, package types, and package exports together. A successful local editor import does not prove that a clean consumer build will work.

Module settings: Keep module and moduleResolution consistent with the runtime and bundler used by the repository. Do not copy a Node-oriented configuration into a browser application without checking how imports are transformed. Frameworks and bundlers may add their own constraints, so treat their generated configuration as part of the compatibility surface.

Path aliases: A paths alias helps TypeScript resolve an import, but it does not automatically rewrite that import for Node.js or a bundler. Prefer package-name imports for workspace packages when the runtime and package exports support them. If aliases are necessary, configure the runtime tool as well.

Output and cache directories: Keep dist, declaration files, and .tsbuildinfo out of source control unless the repository has a specific reason to commit them. Verify that build outputs are ignored and that clean builds remove stale declarations.

Common mistakes

SymptomLikely causeFix
Referenced project must have setting compositeThe referenced package is using an ordinary application configuration.Enable composite and make its files, output, and root directory explicit.
Cannot find a declaration file for a workspace packageThe package has not been built, or its types entry points to the wrong location.Build the dependency first, inspect emitted .d.ts files, and verify the package metadata.
Import resolves to another package's sourceA path alias, editor setting, or package export bypasses the intended build output.Inspect resolution, simplify aliases, and make package exports point to the supported artifacts.
Build succeeds locally but fails in CIStale output or build-info files are masking a missing reference or dependency.Run a clean checkout build with tsc -b --clean followed by tsc -b.
Projects form a circular referenceTwo packages depend on each other directly or through a third package.Move shared types into a lower-level package, invert the dependency, or introduce a boundary between runtime code and types.
File is not listed within the file listA source or test file falls outside the composite project's include patterns.Adjust include or exclude the file intentionally; do not solve the problem by broadly including the whole repository.

Another frequent mistake is confusing a workspace dependency with a TypeScript reference. The package manager can make a package discoverable while tsc -b still lacks the information needed to build it first. Keep both declarations aligned and review them together when moving or renaming a package.

When to revisit

Revisit this checklist whenever you add a package, change a package's public entry point, move source directories, alter module settings, or replace the package manager, bundler, task runner, or TypeScript version. These changes can affect resolution, declaration generation, build ordering, or cache validity even when application code is unchanged.

Before a seasonal planning cycle or a broad dependency upgrade, run a clean root build and record which commands are authoritative for local development and CI. After the upgrade, repeat the same checks rather than comparing only editor diagnostics. Also review the configuration when a team reports slow builds, inconsistent generated types, or differences between local and CI results.

Use this short action list as a maintenance pass:

  1. List every workspace package and its direct consumers.
  2. Check that each referenced project is composite and emits the expected declarations.
  3. Confirm references, package dependencies, package exports, and path aliases describe the same graph.
  4. Delete or clean build outputs, then run tsc -b from the root.
  5. Test one consumer against the built package, not only against source files.
  6. Update the repository documentation when the build command or configuration ownership changes.

A project-reference setup is healthy when a new developer can identify the dependency direction, perform a clean build, and understand where each package's types come from. Treat that clarity as the goal—not simply making one failing command pass.

Related Topics

#typescript#monorepo#project-references#build-tools#workspaces
T

TypeScript.page Editorial Team

Technical Editor

Senior editor and content strategist. Writing about technology, design, and the future of digital media. Follow along for deep dives into the industry's moving parts.