Sub topic 15
Troubleshooting common errors
This chapter assumes you have followed the earlier chapters. You should be comfortable with a base config used through extends, a composite project, the references field, and tsc --build. Most failures in a references setup come from a small set of causes. Once you can read the error code, the fix is usually one line.
A workflow for diagnosing failures
Start with tsc --build --dry --verbose. It prints each project, whether it is up to date, and why it would be rebuilt, without writing any output. Then read the first error only. Later errors in a downstream project are often caused by the first one, because a project that fails to build does not produce the declaration files its dependents need.
Referenced project must have composite
The error looks like this:
error TS6306: Referenced project '/repo/packages/core' must have setting "composite": true.Every project named in references must set composite: true. Add it to the referenced project, not to the one that references it:
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"composite": true,
"rootDir": "src",
"outDir": "dist"
},
"include": ["src"]
}A related error is TS6310, "Referenced project may not disable emit". It appears when the referenced project, or the base config it extends, sets noEmit: true. A referenced project has to emit declarations, so move noEmit out of the base config. Put it only in projects that nothing references, such as a tooling project that only type-checks.
TS6304, "Composite projects may not disable declaration emit", appears if something sets declaration: false. Composite turns declaration on by default, so remove the override.
A file is not listed in the project
error TS6307: File '/repo/packages/core/src/util.ts' is not listed within the file list of project '/repo/packages/core/tsconfig.json'. Projects must list all files or use an 'include' pattern.A composite project must know every file it depends on before it compiles. A file is rejected if it was imported but is not matched by include or files. There are three common causes:
- The
includepattern is too narrow, for example"include": ["src/index.ts"]. Use"include": ["src"]. - The file lives outside the project's directory, for example
../shared/util.ts. - The file is excluded by
exclude, or sits in a folder that theincludeglobs do not match.
The second cause is the most important. Importing another project's source by relative path is allowed only when that project is in references. Without the reference, the compiler treats the file as part of the current project and reports TS6307. It often reports TS6059 as well:
error TS6059: File '/repo/packages/core/src/greet.ts' is not under 'rootDir' '/repo/packages/app/src'. 'rootDir' is expected to contain all source files.Both errors have the same fix. Add { "path": "../core" } to references in the importing project. Do not widen rootDir to ../... That compiles, but it merges the two projects again and defeats the isolation you wanted.
Output file has not been built
error TS6305: Output file '/repo/packages/core/dist/greet.d.ts' has not been built from source file '/repo/packages/core/src/greet.ts'.This one means the setup is correct but the dependency has no output yet. It usually happens when you run plain tsc -p packages/app on a fresh clone. Plain tsc does not build references. It only reads their declaration output, which does not exist.
Run tsc --build packages/app instead. Build mode builds core first and then app. The same error shows up in editors on a fresh checkout. Run one tsc --build at the root and restart the TypeScript server.
Circular references
error TS6202: Project references may not form a circular graph. Cycle detected:
/repo/packages/app/tsconfig.json
/repo/packages/core/tsconfig.jsonBuild mode needs a build order, and a cycle makes an order impossible. The cause is almost always a shared type that both projects want. Break the cycle in one of these ways:
- Move the shared types into a third project that both depend on.
- Invert the dependency so the lower-level project does not import the higher-level one.
- Pass the dependency in at runtime, for example through a function argument or an interface, instead of importing it.
Problems that come from extends
Paths inside a config are resolved relative to the file where they are written. This matters for extends. If you put outDir, rootDir, or include in tsconfig.base.json, each is resolved against the base file's directory. Every project then writes to the same dist at the repository root, and the builds overwrite each other.
You may see TS5055, "Cannot write file ... because it would overwrite input file", or TS6377 about overwriting a .tsbuildinfo file from a referenced project. These are signs that two projects share an output location. Keep per-project paths in the per-project config, and keep only behavior options in the base. The references field is never inherited, so each project must declare its own.
A missing base file gives a different error:
error TS5083: Cannot read file '/repo/tsconfig.base.json'.Check the relative path in extends. From packages/core it is ../../tsconfig.base.json.
Imports that resolve to the wrong thing
When an import from another project fails with TS2307 ("Cannot find module"), check these in order:
| Symptom | Likely cause | Fix |
|---|---|---|
| TS2307 on a relative import | Missing `.js` extension under `NodeNext` | Write `"../../core/src/greet.js"` |
| TS2307 on a package name | Package is not linked in `node_modules` | Add it to `dependencies` and install, or use a relative path |
| Type is `any` or stale | Old `.d.ts` in `dist` | Run `tsc --build` |
| Go to definition lands in `.d.ts` | `declarationMap` is off | Set `declarationMap: true` in the referenced project |
With module: NodeNext, relative imports must include the extension of the output file. That means .js, even though the source file is .ts. This is a module resolution rule, and it applies with or without project references.
Stale or inconsistent output
Build mode decides what to rebuild by comparing timestamps and the hashes recorded in .tsbuildinfo. This normally works. It can go wrong in three situations:
- You deleted
distbut kept a.tsbuildinfofile stored elsewhere throughtsBuildInfoFile. Keep the build info insideoutDir, which is the default whenoutDiris set, so that deletingdistremoves both. - A generated file changed without a timestamp change, for example after a
git checkoutthat restored old timestamps. - You changed compiler options in a way you expected to invalidate everything. Option changes are tracked, but a surprising result is a reason to rebuild.
Use tsc --build --clean and then tsc --build, or tsc --build --force. If a forced rebuild fixes the problem, the cause was stale state. If it does not, the cause is in your configuration. Do not add --force to every build to hide the problem, because it removes the incremental speed-up.
Also check that dist is in .gitignore and that no tool, such as a test runner, writes into it.
Solution config surprises
The root tsconfig.json with "files": [] compiles nothing, so tsc run without --build does nothing and prints nothing. This is expected. If you add include to the root config by habit, the root also becomes a project with its own files, and files then belong to two projects. Keep the root as only files: [] and references.
Another surprise: a project that nothing references is not built by tsc --build at the root unless it is listed there. If your test project never runs, check that the root lists it.
Quick reference
| Code | Meaning | First thing to check |
|---|---|---|
| TS6306 | Referenced project lacks `composite` | The referenced project's `compilerOptions` |
| TS6310 | Referenced project disables emit | `noEmit` in the project or its base |
| TS6304 | Composite project disables declarations | `declaration: false` |
| TS6307 | File not in the project's file list | `include`, `files`, and missing `references` |
| TS6059 | File outside `rootDir` | A missing reference to the project that owns the file |
| TS6305 | Output has not been built | Run `tsc --build` |
| TS6202 | Circular reference | Move shared code to a third project |
| TS5055 | Would overwrite an input file | Distinct `outDir` per project |
Exercise
You clone a repository and run tsc -p packages/app. It reports TS6305 for packages/core/dist/greet.d.ts. Both tsconfig files are correct and app lists core in references. What is the cause, and what do you run?
Show answer
Plain tsc does not build referenced projects, and core has no output yet. Run tsc --build packages/app (or tsc --build at the root) so core is built before app.
Exercise
packages/app/src/main.ts imports ../../core/src/greet.js, but app's tsconfig has no references entry. Which two errors do you expect, and what is the correct fix?
Show answer
TS6307 (the file is not in app's file list) and TS6059 (the file is not under app's rootDir). The fix is to add { "path": "../core" } to references in packages/app/tsconfig.json, not to widen rootDir.
Exercise
Two projects extend a base config that sets "outDir": "dist". Builds keep overwriting each other and you see TS5055. Why, and how do you fix it?
Show answer
Paths in an extended config resolve relative to the base file, so both projects write to the same dist at the repository root. Remove outDir (and rootDir) from the base and set them in each project's own tsconfig.