Sub topic 17
Project references for multi-package repositories
This chapter assumes you can write a basic tsconfig.json, understand outDir, rootDir, and declaration, and know how extends shares settings. It also assumes you have used npm workspaces or a similar monorepo layout. Here you will split one repository into several TypeScript projects that build in dependency order.
Why one tsconfig stops scaling
A single tsconfig.json compiles everything as one program. In a repository with several packages, this causes three problems:
- Every change re-checks all files, even in packages that did not change.
- Nothing stops
appfrom importing the internals ofcorethrough a relative path such as../../core/src/internal. - Output from several packages ends up under one
outDir, which blurs package boundaries.
Project references fix this. Each package becomes its own TypeScript project with its own tsconfig.json. A project declares the projects it depends on, and tsc --build compiles them in dependency order. When a project is unchanged, the compiler skips it.
The working model
A referenced project is compiled first, and its emitted .d.ts files stand in for its source. When app imports from core, the compiler reads core's declaration output instead of re-parsing and re-checking core's source. That saves time and enforces the boundary: app can see only what core publishes.
Three settings make this work:
composite: truein the referenced project.- A
referencesarray in the project that depends on it. - Building with
tsc -b(or--build), not plaintsc.
A two-package example
The layout is a repository with two packages and a shared base config:
tsconfig.base.jsontsconfig.json(the solution file)package.json(workspaces)packages/core/withpackage.json,tsconfig.json, andsrc/index.tspackages/app/withpackage.json,tsconfig.json, andsrc/main.ts
The root package.json enables workspaces, so npm links each package into node_modules under its package name:
{
"private": true,
"workspaces": ["packages/*"]
}The core package points its entry fields at the build output:
{
"name": "@acme/core",
"version": "1.0.0",
"main": "dist/index.js",
"types": "dist/index.d.ts"
}The app package depends on core:
{
"name": "@acme/app",
"version": "1.0.0",
"dependencies": {
"@acme/core": "*"
}
}The source files are small:
// packages/core/src/index.ts
export function greet(name: string): string {
return `Hello, ${name}`;
}// packages/app/src/main.ts
import { greet } from "@acme/core";
console.log(greet("tsconfig"));The core project marks itself composite:
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"composite": true,
"declarationMap": true,
"outDir": "dist",
"rootDir": "src"
},
"include": ["src"]
}The app project is also composite and lists core in references:
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"composite": true,
"outDir": "dist",
"rootDir": "src"
},
"include": ["src"],
"references": [{ "path": "../core" }]
}The root tsconfig.json is a solution file. It compiles nothing itself and only lists the projects to build:
{
"files": [],
"references": [
{ "path": "packages/core" },
{ "path": "packages/app" }
]
}The "files": [] line matters. Without it, the root config would fall back to the default of including every .ts file under the root, and would compile all of them as a third project.
Build everything from the repository root:
npx tsc -bBecause app references core, the compiler builds core first, then app.
What composite changes
Setting composite: true is not only a label. It adds constraints that make incremental builds reliable:
declarationdefaults totrue, so every project emits.d.tsfiles.incrementalis on, and the compiler writes a.tsbuildinfofile that records file hashes and the state of the last build.rootDirdefaults to the directory containing thetsconfig.jsonif you do not set it. Set it explicitly, as above, so output paths stay predictable.- Every source file must be matched by
includeorfiles. A file the compiler reaches only through an import is an error.
The last rule exists because the compiler decides whether a project is out of date by looking at its declared file list. A file that is not in that list would be invisible to the up-to-date check.
The build commands
| Command | What it does |
|---|---|
| `tsc -b` | Builds the project and every referenced project that is out of date |
| `tsc -b --verbose` | Same, and explains why each project was built or skipped |
| `tsc -b --watch` | Rebuilds affected projects when files change |
| `tsc -b --clean` | Deletes the outputs of the project and its references |
| `tsc -b --force` | Rebuilds everything, ignoring up-to-date checks |
tsc -b accepts a path to a project directory or config file, such as npx tsc -b packages/app. This builds app and its references and nothing else.
Plain tsc -p packages/app does not build references. It type-checks app against the declaration files that core has already emitted. If those files do not exist yet, you get error TS6305.
What references do not inherit
extends and references are separate mechanisms:
extendscopies compiler options and, unless overridden,include,exclude, andfiles. Paths in the base file are resolved relative to the base file.referencesis never inherited. Each project lists its own dependencies.compositecan live in a shared base, but this is usually a bad idea. It would also apply to the solution file and to any config that is not meant to be a buildable project. Setting it per package is clearer.
Common errors
| Error | Cause | Fix |
|---|---|---|
| TS6306: Referenced project must have setting "composite": true | A project in `references` does not enable `composite` | Add `composite: true` to the referenced project |
| TS6307: File is not listed within the file list of project | A composite project imports or contains a file not matched by `include` or `files` | Widen `include`, or move the file into the project |
| TS6059: File is not under rootDir | A source file sits outside `rootDir` | Move the file, or change `rootDir` |
| TS6305: Output file has not been built from source file | Plain `tsc` ran before the referenced project was built | Run `tsc -b` |
| Cannot find module '@acme/core' | The workspace link is missing, or `types` in `core/package.json` is wrong | Run `npm install` and check the `types` path |
Gotchas
Stale output after deleting files. If you remove a source file, its old output in dist can remain. Run tsc -b --clean and then rebuild when output looks wrong.
noEmit conflicts. A composite project has to emit declarations, so you cannot set noEmit: true on it. For a quick type-check across the repository, use tsc -b and accept the emit, or keep a separate non-composite config for checks.
Editors and go-to-definition. By default, jumping to a symbol in a referenced project lands in the .d.ts file. Setting declarationMap: true in the referenced project, as in the core config above, lets the editor jump to the original .ts source.
Circular references. Project A cannot reference B if B references A. The compiler reports a cycle error. Extract the shared code into a third project that both depend on.
Build order is not import order. The order comes from references, not from your import statements. If app imports @acme/core but does not list it in references, a clean tsc -b can build app first, and the import fails because core's declarations do not exist yet.
When to use them
Project references pay off when a repository has several packages with clear dependency directions, or when a full type-check has become slow. For a single-package project or a small repository, a single tsconfig.json is simpler. The cost of references is extra config files and a mandatory composite setup.
To check what the compiler uses for a package, run npx tsc -p packages/app --showConfig. It prints the resolved options and the references list. Use npx tsc -p packages/app --listFiles to confirm that only the files you expect are in the project.
Exercise
You add a new package packages/utils and import it from packages/app as @acme/utils. After a clean checkout, npx tsc -b fails in app because the declarations for utils cannot be found. The utils config has composite: true. What is missing, and where do you add it?
Show answer
The app project does not list utils in its references array, so tsc -b does not know to build utils first. Add { "path": "../utils" } to references in packages/app/tsconfig.json. Also add { "path": "packages/utils" } to the root solution file so that npx tsc -b from the root builds it directly.
Exercise
A teammate puts "composite": true in tsconfig.base.json and tsc -b now reports TS6307 for a config that was never meant to be a project. Why does it happen, and what is a better arrangement?
Show answer
Every config that extends the base inherits composite, including configs with a broad include or files that sit outside their own project's list, and composite projects must list all their files. Remove composite from the base and set it only in each package's tsconfig.json.