Sub topic 9
Building with tsc --build
This chapter covers build mode, the tsc --build command (also written tsc -b). It assumes you can read a tsconfig.json, know what outDir and declaration do, and have a repository where packages/app references packages/core, both with composite enabled. Earlier chapters set up that layout. This chapter explains how the compiler builds it.
Why build mode exists
Plain tsc -p packages/app compiles one project. It reads the referenced project's emitted .d.ts files, but it never builds them. If packages/core/dist is missing or stale, you get errors, or worse, you type-check against old declarations.
tsc --build fixes this. It treats the set of projects as a graph and does four things:
- Finds every project reachable through
references. - Checks whether each project's outputs are up to date.
- Builds out-of-date projects in dependency order.
- Skips projects whose inputs have not changed.
It behaves differently from tsc -p in other ways too. It always emits (so noEmit on a composite project causes problems), and it stops building downstream projects when an upstream project has errors.
The basic commands
Run these from the repository root. The root tsconfig.json is the solution-style file from the previous chapter, which lists both projects and compiles nothing itself.
# Build everything in dependency order, skipping up-to-date projects
tsc --build
# Show what would be built and why, without building
tsc --build --dry --verbose
# Rebuild everything from scratch
tsc --build --force
# Delete the outputs of all projects
tsc --build --clean
# Rebuild on file changes
tsc --build --watchYou can also point build mode at one project. tsc -b packages/app builds app and everything it references, but not unrelated projects. With no argument, tsc -b uses the tsconfig.json in the current directory.
Build-only flags
Build mode accepts a limited set of flags of its own:
| Flag | Effect |
|---|---|
| `--verbose` | Prints each project's status and the reason it is or is not rebuilt |
| `--dry` | Reports what would happen without writing any files |
| `--force` | Ignores up-to-date checks and rebuilds every project |
| `--clean` | Deletes the outputs of the listed projects |
| `--watch` | Keeps running and rebuilds affected projects after file changes |
| `--stopBuildOnErrors` | Stops the whole build at the first project with errors |
Most other compiler options must live in tsconfig.json, not on the command line. Passing options such as --outDir on a tsc -b command line is not supported. The one exception is the small set of general flags (like --incremental or --pretty) that apply to the whole build.
How the build proceeds
Consider the two-project layout. The root solution file lists core and app, and app references core.
For each project, the compiler compares timestamps and the .tsbuildinfo file. A project is out of date when any of these is true:
- An output file is missing, such as
dist/greet.jsordist/greet.d.ts. - An input source file is newer than the oldest output.
- A referenced project was rebuilt and its declaration output changed.
- The
.tsbuildinfofile is missing or records different compiler options.
That last point matters for speed. If you edit only a function body in core, core recompiles. Its .d.ts output is identical, so recent TypeScript versions can skip a full re-check of app and just update timestamps. Changing an exported signature makes app rebuild.
Reading --dry --verbose output
The --dry --verbose combination is the best way to learn what build mode decides. On a fresh checkout, with no dist folders, the output looks like this:
[12:00:00] Projects in this build:
* packages/core/tsconfig.json
* packages/app/tsconfig.json
* tsconfig.json
[12:00:00] A non-dry build would build project '/repo/packages/core/tsconfig.json'
[12:00:00] A non-dry build would build project '/repo/packages/app/tsconfig.json'After a real build, run it again and it changes to messages such as "Project 'packages/core/tsconfig.json' is up to date because newest input 'packages/core/src/greet.ts' is older than output 'packages/core/dist/greet.js'". Exact wording varies between TypeScript versions, but each line names a project and a reason.
Errors and partial builds
When a project has type errors, build mode reports them and does not build projects that depend on it. In older TypeScript versions, a project with errors emitted nothing at all, so downstream projects saw no declarations. Starting with TypeScript 5.6, build mode continues past errors and still emits where it can, unless you pass --stopBuildOnErrors.
Two consequences to keep in mind:
- A green exit code means every project in the build succeeded. Check the exit status in scripts, not just the absence of output.
- If
corefails, errors inappthat come from missingcoretypes are noise. Fixcorefirst, then rebuild.
Clean and force are different
--clean deletes outputs: the emitted .js, .d.ts, .d.ts.map, and .tsbuildinfo files. It does not rebuild anything. --force rebuilds everything and overwrites outputs, but does not delete files that no longer correspond to a source file.
That difference creates a common stale-output problem. If you delete packages/core/src/old.ts, dist/old.js and dist/old.d.ts remain after tsc -b. They are harmless to the compiler, but a bundler or test runner that globs dist will pick them up. To remove them, run tsc -b --clean and then tsc -b.
tsc -b --clean
tsc -bYou can combine --clean with a project path to clean one project and its references, for example tsc -b --clean packages/app.
Watch mode
tsc -b --watch builds once, then keeps a watcher on every project in the graph. When you save a file in core, it rebuilds core and then any project that depends on it. Projects that are unaffected stay untouched.
Watch mode has limits worth knowing:
- Adding or removing a
referencesentry in atsconfig.jsonis picked up, but restarting the watcher is the reliable fix if the graph looks wrong. - Configuration changes to a shared base file, reached through
extends, trigger a rebuild of the projects that extend it. - Each project keeps its own
.tsbuildinfo, so a restart of the watcher is quick.
Build mode and the incremental model
Build mode implies incremental for composite projects. Each project writes a .tsbuildinfo file recording file hashes, the dependency graph of source files, and the options used. By default it goes next to the output directory or inside outDir. If two projects share an outDir, their buildinfo files collide, so give each project its own outDir (or set tsBuildInfoFile).
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"composite": true,
"rootDir": "src",
"outDir": "dist",
"tsBuildInfoFile": "dist/.tsbuildinfo"
},
"include": ["src"]
}Commit neither dist nor .tsbuildinfo. Add both to .gitignore. A stale buildinfo checked in from another machine can make the compiler believe outputs are current when they are not.
Common problems with build mode
| Symptom | Likely cause | Fix |
|---|---|---|
| `Referenced project must have setting "composite": true` | A project in `references` lacks `composite` | Enable `composite` in the referenced project |
| `File is not listed within the file list of project` | A source file is outside `include` or `files` | Add it to `include`, or move it into `src` |
| App still sees old types after editing core | Core's build failed or was not run | Run `tsc -b` and read core's errors first |
| Old files remain in `dist` | `--force` does not delete outputs | Run `tsc -b --clean`, then `tsc -b` |
| `tsc -b` rebuilds everything every time | Shared `outDir`, a changing generated file, or a clock skew | Run `--dry --verbose` and read the stated reason |
Putting it together
Use tsc -b for every build, in scripts and in your terminal. Use --dry --verbose to understand its decisions, --clean followed by a build to remove stale files, and --watch during development. Keep each project's outputs separate, keep outputs out of version control, and treat any error in an upstream project as the first thing to fix.
Exercise
You delete packages/core/src/legacy.ts, then run tsc -b --force. A test runner that globs packages/core/dist/**/*.js still runs legacy.js. Why does this happen, and what two commands fix it?
Show answer
--force rebuilds every project but does not delete outputs that no longer have a source file, so dist/legacy.js and dist/legacy.d.ts remain. Run tsc -b --clean to delete all project outputs, then tsc -b to rebuild them from the current sources.
Exercise
A teammate runs tsc -p packages/app in a CI script after a fresh checkout and sees errors that say the module core cannot be found or has no declarations. Why does tsc -b packages/app avoid this?
Show answer
-p compiles only app and expects core's declaration files to already exist in core/dist. On a fresh checkout they do not. tsc -b packages/app follows references, builds core first to produce its .d.ts files, and then builds app against them.