Sub topic 15
Troubleshooting Common Errors in TypeScript Project References
Assume you know basic TypeScript syntax, tsconfig.json, ES modules, and the tsc command. This chapter uses the two-project layout from the earlier examples: packages/core is the library, and packages/app depends on it.
Project references add rules that a single-project configuration does not have. Most errors come from one of three causes:
- A referenced project is not configured as a buildable project.
- A source file is outside the project’s declared input set.
- The dependency graph or generated output is inconsistent.
Check the project that reports the error
Start with the exact project named in the diagnostic. Run its configuration check and inspect the build graph:
tsc --project packages/core/tsconfig.json --showConfig
tsc --build --dry --verboseThe first command shows the options after extends is resolved. The second command explains which projects TypeScript would build and why.
| Error or symptom | Likely cause | First fix to try |
|---|---|---|
| Referenced project must have `composite: true` | The dependency is not a composite project | Add `composite` to the dependency |
| File is not listed within the file list | `include` or `files` excludes a source file | Correct the project’s input pattern |
| Cannot find declarations | The dependency has not emitted usable output | Build the dependency and check `outDir` |
| Circularity detected | References form a cycle | Remove or redesign one dependency |
| Changes seem ignored | Stale `.tsbuildinfo` or output files | Run a forced build or clean build |
Fix composite and file-list errors
Every project named in references must enable composite. Composite projects also require TypeScript to know every source file that belongs to the project.
A typical library configuration looks like this:
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"composite": true,
"declaration": true,
"rootDir": "src",
"outDir": "dist"
},
"include": ["src/**/*.ts"]
}If src/internal/helper.ts is excluded by an overly narrow include pattern, the build can report that the file is not listed. Either widen the pattern or add the file explicitly with files.
A file may also be included accidentally. For example, placing tests under src means the library project may try to compile them. Use separate directories or an explicit test project when test files need different settings.
Fix output and import problems
A referenced project normally provides declaration files, such as dist/greet.d.ts, to its dependents. If those files are missing, build the dependency first:
tsc --build packages/core/tsconfig.json
tsc --build packages/app/tsconfig.jsonCheck that the importing project references the directory containing the dependency’s tsconfig.json, not its src directory. Also verify that the dependency’s outDir is not ignored or deleted before the dependent project runs.
With NodeNext, keep import specifiers compatible with emitted JavaScript:
import { greet } from "../../core/src/greet.js";
console.log(greet("Ada"));For published packages, prefer the package name and configure its exports and declaration entry points. Direct source imports can bypass the package boundary and create confusing resolution behavior.
Keep the dependency graph acyclic
References describe a directed graph. If app references core, core cannot reference app. Move shared types into a third project, or invert the dependency.
Repair stale incremental state
Build mode stores dependency information in .tsbuildinfo files. They speed up rebuilds, but an interrupted build or changed configuration can leave confusing results.
tsc --build --clean
tsc --build --force
tsc --build --verboseUse --clean when you want to remove generated outputs, then use a normal build. Use --force when outputs exist but TypeScript must rebuild every project.
Exercise
packages/app references packages/core, but TypeScript reports that the referenced project must have composite: true. What should you change?
Show answer
Add "composite": true to packages/core/tsconfig.json, ensure its source files are covered by include or files, and rebuild the dependency.