TypeScript TS5055: Cannot write file because it would overwrite input
Fix TS5055 when the compiler output directory overlaps the source directory or includes previously emitted files.
Symptoms
- error TS5055: Cannot write file ... because it would overwrite input file
- The error appears after changing outDir or rootDir
- Common in monorepos with shared tsconfig files
Possible causes
- outDir is not set, so emitted files land next to sources and are picked up as inputs
- The include glob matches already-compiled JavaScript
- A parent tsconfig emits into a child project directory
- allowJs pulls generated files into the program
Diagnosis steps
Print the emitted file layout versus inputs
tsc --listEmittedFiles --noEmit falseInterpretation
Overlap between emitted and input paths confirms the conflict.
Check outDir and rootDir
Interpretation
Setting outDir outside the source tree separates inputs from outputs.
Inspect include/exclude globs
Interpretation
Compiled .js files matched by include become inputs and are then overwritten.
Root cause
TypeScript refuses to overwrite a file that is also part of the compilation input, because that would corrupt the program. This happens when outDir is unset or points inside the source tree, or when include matches previously emitted files, so output and input sets intersect.
Fix
Set outDir to a directory outside the source tree and rootDir to the source root, then exclude the output directory from include. In project references, give each project its own outDir. Delete stale emitted files so they are not re-ingested as inputs, and let the build own a clean output directory.
Verify the fix
Run tsc again; it emits into the separate outDir with no TS5055, and listing emitted files shows no path inside the source directory.
Symptoms
error TS5055: Cannot write file ... because it would overwrite input file appears, often right after you change compiler options or add allowJs.
Diagnosis
Compare outputs and inputs:
tsc --listEmittedFiles
If an emitted path also appears among inputs, the sets overlap.
Fix
Separate output from source:
{
"compilerOptions": {
"rootDir": "src",
"outDir": "dist"
},
"include": ["src/**/*"],
"exclude": ["dist", "node_modules"]
}
Remove stale compiled files from the source tree.
Verification
Re-run tsc; it writes to dist without TS5055, and –listEmittedFiles shows no output path inside src, confirming inputs and outputs are disjoint.
Overlap in Monorepos
Shared tsconfig files make overlap subtle: a base config with no outDir, extended by projects that add their own, can leave one project emitting next to its sources. The symptom is that the first build succeeds and the second fails with TS5055, because emitted JavaScript from run one is now an input. A clean output directory per project, plus excluding that directory in include, is the structural fix. Committing a .gitignore for the output and deleting stale artifacts before a fresh build keeps the input set stable across incremental and clean runs.