Sub topic 10
A solution-style root tsconfig
This chapter assumes you have already built a two-project setup like the one in the previous chapters. Each project has composite: true, core is listed in app's references, and a shared tsconfig.base.json sits at the repository root. It also assumes you can run tsc --build.
A solution-style root tsconfig.json is a config file that compiles nothing itself. It only lists the projects that make up the repository. This chapter covers what it contains, why it is written that way, and what goes wrong when it is not.
The problem a root config solves
Once a repository has several projects, you want a single command at the root that builds all of them. You also want your editor and other tools to behave sensibly when they open a file anywhere in the repository.
Without a root tsconfig.json, you have to name each project yourself:
tsc --build packages/core packages/appThis works, but every new package means editing every script and CI step that builds the repository. A root config moves that list into one file that tsc --build reads on its own.
The shape of a solution config
The solution config has two properties. files is an empty array, so the project contains no source files. references lists every project that belongs to the solution.
{
"files": [],
"references": [
{ "path": "./packages/core" },
{ "path": "./packages/app" }
]
}Each path points to a directory that contains a tsconfig.json, or directly to a config file such as ./packages/app/tsconfig.test.json.
With this file in place, one command builds the whole repository:
tsc --buildWith no arguments, tsc --build looks for tsconfig.json in the current directory. It reads the references, orders the projects so that dependencies come first, and builds each one that is out of date. Because the root has no files, it produces no output of its own.
Why `files` must be an empty array
If a tsconfig.json has neither files nor include, the compiler includes every .ts file under the config's directory by default. In a solution config that is a mistake. The root would become a project that type-checks the whole repository as one program, which is exactly what project references are meant to avoid.
Writing "files": [] turns that default off. Normally an empty files list is an error, but the compiler allows it when references is present. That exception is what makes this pattern work.
You can get a similar result with "include": []. The files form is the conventional spelling and is what the TypeScript documentation and most repositories use.
What the root does not need
The solution config is usually minimal. These are the points people most often get wrong:
- No
extends. The root compiles nothing, so shared options have no effect on it. Each project extends the base config on its own. - No
composite. Thecompositeflag is required only on projects that other projects reference. Nothing references the root. - No output directory. The root has no outputs, so
outDiranddeclarationare meaningless there.
An exception is a root that also holds code, such as a single src folder next to packages. In that case, move that code into its own project (for example tsconfig.root.json) and reference it from the solution config like any other project.
How the root fits into the dependency graph
The root points at every project, and the projects point at each other through their own references. The root does not need to repeat those dependency links. Listing app alone would still build core, because app references it. Listing both is still recommended, because the editor looks only at the root's list when it searches for a project that owns a file (see the next section).
How the editor uses the root
When you open a file, the editor's TypeScript server (tsserver) searches upward from that file for the nearest tsconfig.json. In a monorepo that is usually the package's own config, which is fine.
The solution config matters when the nearest config does not include the file. A common case is a file under packages/app that the app's tsconfig.json excludes, or a repository that has a single root config at the top. If the config it finds is solution-style (empty files with references), tsserver follows the references and uses the first referenced project that includes the file. This is called solution searching.
If the file belongs to none of the referenced projects, the editor falls back to treating it as part of an inferred project. In that mode it uses default compiler options, so you see unexpected errors such as missing strict checks or unresolved imports. In practice, a file that behaves strangely only in the editor often belongs to no project. Check that some referenced project's include covers it.
Two compiler options tune this behavior. You set them in the config that tsserver loads, which is the solution config here:
{
"files": [],
"compilerOptions": {
"disableSolutionSearching": true
},
"references": [
{ "path": "./packages/core" },
{ "path": "./packages/app" }
]
}disableSolutionSearchingstops a project from taking part in the editor's search through references.disableReferencedProjectLoadstops the editor from loading every referenced project at startup, which helps in very large repositories.
Most repositories do not need either option. They are useful when the editor is slow because it loads too many projects, or when a project should never be picked as the owner of unrelated files.
Running a single project and its dependencies
You do not have to build everything. Passing a project path to tsc --build builds that project and the projects it depends on, and nothing else:
# Builds core first (because app references it), then app
tsc --build packages/app
# Builds only core
tsc --build packages/core
# Builds everything the root lists
tsc --buildThe same applies to --clean, --force, and --watch. For example, tsc --build --clean packages/app deletes the outputs of app and of the projects it depends on, but it leaves projects outside that chain alone.
A typical package.json at the repository root wraps these commands:
{
"name": "tutorial-monorepo",
"private": true,
"scripts": {
"build": "tsc --build",
"build:app": "tsc --build packages/app",
"clean": "tsc --build --clean",
"watch": "tsc --build --watch"
},
"devDependencies": {
"typescript": "^5.4.0"
}
}Other tools and the root config
Many tools look for a file named tsconfig.json in the working directory and assume it describes real source files. Against a solution config, they find an empty program. Typical symptoms:
tsc -p .or plaintscat the root does nothing useful, because there are no files and no--buildflag. Without--build,tscdoes not follow references for compilation order.- Linters that use type information, such as typescript-eslint with
parserOptions.project, need a config that lists the files to lint. Point them at the per-package configs, or at a separatetsconfig.eslint.jsonthat includes the files you want checked. - Test runners and transpilers such as ts-jest or ts-node read one config. Give them a config from the package they run in, not the root.
The fix is the same in each case: tell the tool which project to use, rather than adding source files to the solution config.
Adding a project
Adding a new package takes three steps. These are the whole maintenance cost of the solution config:
- Create
packages/<name>/tsconfig.jsonthat extends the base config and setscomposite: true,rootDir,outDir, andinclude. - Add
{ "path": "./packages/<name>" }to the rootreferences. - Add a
referencesentry in any project that imports from the new package.
If you skip step 2, tsc --build at the root may still build the new project when another project references it. The editor, however, will not find it through the root, and a file in the new project that no other config claims can fall back to an inferred project.
Common mistakes
| Symptom | Likely cause | Fix |
|---|---|---|
| Root `tsc --build` also compiles every file and produces duplicate output | The root has no `files: []` and no `include`, so it defaults to all files | Add `"files": []` |
| Error that a referenced project must have `composite` enabled | A listed project lacks `composite: true` | Set `composite: true` in that project |
| Editor shows type errors that `tsc --build` does not | The file belongs to no referenced project and is treated as inferred | Include the file in a project's `include`, and make sure the root lists that project |
| A new package is never built from the root | Its path is missing from the root `references`, and nothing references it | Add the path to the root `references` |
| Linter reports "file not included in project" | The tool is reading the empty root config | Point it at a per-package config |
Summary of the pattern
A solution-style root is small on purpose. Its job is to name the projects, and the projects themselves hold all compiler settings and file lists. That keeps one source of truth for what is built (the per-project configs), one command for building it (tsc --build), and a single place the editor can start from when it looks for the owner of a file.
Exercise
A teammate adds a new package packages/utils with its own composite tsconfig.json, and packages/app now imports from it. They add { "path": "../utils" } to packages/app/tsconfig.json but do not touch the root tsconfig.json. tsc --build at the root succeeds. A week later, someone opens a file in packages/utils that is excluded by its own config and sees odd type errors in the editor. Name two things that explain why the root should have listed utils, and state the one change that fixes the root.
Show answer
First, the editor's solution search starts from the root's references, so a project missing from that list cannot be found as the owner of a file, and the file falls back to an inferred project with default options. Second, the root is the list of projects that other commands read, such as tsc --build --clean and tsc --build --watch at the root. The build only worked because app referenced utils, so adding another entry point that does not reach utils would skip it. The fix is to add { "path": "./packages/utils" } to the references array in the root tsconfig.json.
Exercise
Why is "files": [] in a solution config not an error, even though the compiler normally reports an empty files list, and what would happen if you removed it from a root config that has no include?
Show answer
The compiler allows an empty files list when the config has references, because the config's purpose is then to coordinate the referenced projects. If you removed it and had no include, the default include would apply and the root would become a project that type-checks every .ts file under the repository, overlapping the real projects and defeating the point of splitting them.