TypeScript: Error "Cannot write file ... because it would overwrite input file."
TypeScript Version: 2.2.1
When using Visual Studio 2015 Update 3 I am getting hundreds of errors in the error list like:
Cannot write file ‘C:/{{my-project}}/node_modules/buffer-shims/index.js’ because it would overwrite input file.
It looks like this all the time. It doesn’t actually prevent building, and everything works just fine, but the error list is distracting and difficult to locate “real” errors when they occur.

My tsconfig.json file
{
"compileOnSave": true,
"compilerOptions": {
"baseUrl": ".",
"module": "commonjs",
"noImplicitAny": true,
"removeComments": true,
"sourceMap": true,
"target": "ES5",
"forceConsistentCasingInFileNames": true,
"strictNullChecks": true,
"allowUnreachableCode": false,
"allowUnusedLabels": false,
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"typeRoots": [],
"types": [] //Explicitly specify an empty array so that the TS2 @types modules are not acquired since we aren't ready for them yet.
},
"exclude": ["node_modules"]
}
How can I get rid of all these errors?
(I’ve also posted this question on StackOverflow with no responses yet)
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 174
- Comments: 110 (14 by maintainers)
Commits related to this issue
- added dist as exclude (fix https://github.com/Microsoft/TypeScript/issues/14538 ) — committed to ShyykoSerhiy/spotilocal by ShyykoSerhiy 7 years ago
- Changed "noEmit" to "true" to fix this issue: https://github.com/Microsoft/TypeScript/issues/14538 — committed to ataravati/mobile-boilerplate by ataravati 5 years ago
- chore: update tsconfig to avoid some errors Issue and comment for its fix. <https://github.com/Microsoft/TypeScript/issues/14538#issuecomment-302487769> — committed to takayukioda/ham by takayukioda 5 years ago
- chore: update tsconfig to avoid some errors Issue and comment for its fix. <https://github.com/Microsoft/TypeScript/issues/14538#issuecomment-302487769> — committed to takayukioda/ham by takayukioda 5 years ago
- chore: update tsconfig to avoid some errors Issue and comment for its fix. <https://github.com/Microsoft/TypeScript/issues/14538#issuecomment-302487769> — committed to takayukioda/ham by takayukioda 5 years ago
- fix(tsconfig): could not overwrite files in dist re https://github.com/Microsoft/TypeScript/issues/14538 — committed to technologiestiftung/flusshygiene-utils by ff6347 5 years ago
- Fix editor warning See https://github.com/Microsoft/TypeScript/issues/14538 — committed to otiai10/lookpath by otiai10 5 years ago
- fix: js file problems when allowJs reference: https://github.com/microsoft/TypeScript/issues/14538#issuecomment-427979741 — committed to yunsii/ant-design-mobile-pro by yunsii 5 years ago
- allowJs: trueによるyarn docエラー解消 https://github.com/microsoft/TypeScript/issues/14538#issuecomment-427979741 — committed to miyanokomiya/okageo by miyanokomiya 5 years ago
- :bug: building doesn't work Fails with "error TS5055: Cannot write file '/Users/astoilkov/Repos/use-local-storage-state/dist/index.d.ts' because it would overwrite input file.". - https://stackoverfl... — committed to astoilkov/use-local-storage-state by astoilkov 4 years ago
- fix: fix strange ts error in circleci https://github.com/microsoft/TypeScript/issues/14538#issuecomment-514990811 — committed to aurelia/cli by 3cp 4 years ago
- fix for error writing tsconfig.json https://github.com/microsoft/TypeScript/issues/14538#issuecomment-559074255 — committed to marcalexiei/ractive by marcalexiei 4 years ago
- chore: fix Cannot write file ... because it would overwrite input file. issue: https://github.com/microsoft/TypeScript/issues/14538 — committed to koexjs/koex by deleted user 4 years ago
- fix: Do not import recursively cozy-client Importing recursively leads to tsc errors > Cannot write file 'client/packages/cozy-client/types/models/note.d.ts' because it would overwrite input file. ... — committed to cozy/cozy-client by ptbrowne 3 years ago
- Set outdir in tsconfig.json Due to the way that typescript works in nodejs module resolution, the index.d.ts file will load .ts files incorrectly if you publish the .js and .d.ts files in alongside t... — committed to jamie-pate/typed-cli by jamie-pate 3 years ago
I found a fix for me. In my case I used
outDirandrootDirwithout specifyingfilesarray. When adding the path ofoutDirto theexcludearray everything seems to be working normally.Maybe TypeScript is watching over the content of the
distfolder as well even though it’s set as theoutDir.Fixed it for me.
Setting:
fixed it for me (my
outputDirwasdist).It could be awesome if the output dir is excluded by default for
tsc --watch.Solved - had been including dist directory in my tsc build:
— goes to
"allowJs": true"noEmit": truework for me.Just add you “dist” folder to the exclusions list in tsconfig.json ex: “exclude”: [“node_modules”, “dist”]
I just stumbled across this issue while having the same problem, and thought I’d add what mine turned out to be in case other people stumble across it the same way.
In my case, I have a monorepo with a separate tsconfig file for each package, that all extend from a base tsconfig. Each packages config has
referencesentries that point to the path of the packages it depends on. I also have a tsconfig.json in the root of the repo that includesfiles: []and has references to all of the package directories. This way I can runtsc -b --watchfrom the root and have it rebuild on changes throughout the entire project.This worked fine for quite a while, then abruptly started throwing this error even though none of the configs had changed.
I finally tracked it down by attempting to build the one package that was being reported in the error by itself, rather than building the whole project.
Turns out the problem was that I had attempted to have the project import itself. The package name was
@my-project/utils, and it worked fine until I moved some code from another package into a file in the utils package. That code includedimport stuff from '@my-project/utils';and that was what caused the error. By changing to toimport stuff from '.';instead the error went away…Excluding my build dir solved the issue for me
in Vue project, in the root, this worked for me:
After i added the
"outDir": "",problem was gone.My gole here is just to make ts intellisence work in .js/vue files.
is
--allowjsset? can you share the project?I feel like this issue is related to these:
The issue is happening to me because I set
"declaration": truein thetsconfig.json, so it gets angry about thed.tsfiles in the build folder, even though the directory is outside of the root. I can do a fresh build w/o issues, but any build after that will throw:Cannot write file ... because it would overwrite input file.. From what I could see on the other stories, this used to work in another version, then something must have changed.Just like I fixed the issues about my
test.tsfiles being outside of therootDirI had to add the following to thetsconfig.json.This can also happen with config inheritance. Each config will need to specify
outDirseparately. It appears that the path inoutDiris resolved in absolute terms. Might even be a bug from the user’s point of view.If you are using TS just for type checking ONLY (no compilation) and you need it in
.jsfiles, use @guaizi149 solution:This will tell TS that it shouldn’t worry about compilation, therefore no file will be over written and no warning will be triggered. This is a better solution to using
outDir: "".I ran into the same issue and found that one of my imports was incorrectly referencing the class in my dist folder E.G import {ClassName} from “…/…/dist/ClassName”;
As the importing class was in the same folder I changed it to: import {ClassName} from “./ClassName”;
and everything is compiling again 😃
This happens when the declaration files are not excluded from the build. Whenever this occurs, the builder tries to build the existing “.d.ts” files and replace them with the same filename. So that’s why you will get the error:
Cannot write file ... because it would overwrite input file.To prevent this, you can exclude your
"outDir":"build"in jour tsconfig.json file:or if you don’t have outDir defined exclude all
d.ts. extension files:Hope this helps
yup, as you can see in the issue the output is from running raw
tscsecond time.just clone the repo https://github.com/wc-catalogue/blaze-elements
yarnfrom rootyarn tsc-> first time compilation ( everything’s ok ) => first timedefinitions/folder generatedyarn tscagain -> errorsbest solution is from borislemke .
{ "compilerOptions": { ..., "outDir": "./dist", "rootDir": "./src", }, "exclude": [ "node_modules", "dist" <-- I had to add this to fix the errors ] }Sorry, I cannot share the project, and no that flag is not set. My
tsconfig.jsonis above, and we just use that with VS2015 Update 3, which just triggers the build with MSBuild normally.I have teammates complaining about the same issue happening to them on the same projects. I also work on a project at home on a different computer that has the exact same issue & the same setup (TS 2.2.1, VS2015 U3, etc.)
If you want to test another solution and check if it works, here is mine:
Check if these conditions are met for your project:
allowJs: trueanddeclaration: truein your tsconfigpackage.jsonmainfile to the compiled js file (e.g.main: './dist/index.js')index.tsusing relative imports (e.g.import something from '../..') somewhere in your projectThen try changing from
to
Hope it helps.
To anyone came here by googling a similar error message, let me share my findings:
allowJs: truefrom your tsconfig.json or--allow-jsfrom your CLI options.exclude: ....compilerOptions.Hope it helps 🙏
For future reference, this happens if you import a module from within itself.
So doing
import x from 'mymodulewhen insidemymodulewill trigger this. It’s very cryptic, and likely should be fixed!I corrected this by adding an Include section:
"include": [ "*.ts", ], "exclude": [ "node_modules" ]I also get same problem.
Commenting here because it is the first thing that came up in my google search.
I ran into this issue too. It seems like some part of the compilation process isn’t recognizing that it is inside an excluded directory.
I don’t don’t see the problem if I do this:
I do see the problem if I do this:
or this:
The error occurs despite the fact that the files it is complaining about are clearly inside
dist:In my case I have imports set up where
src/index.tsimports and re-exports fromsrc/matchers/index.tswhich in turn imports and re-exports fromsrc/matchers/eitherMatchers/index.ts.The first two files are the ones causing the compilation errors. The third file is fine. So it looks like it might be related to how the import / export tree is affecting compilation.
Like @uglycoyote what worked for me was to add the
outdirto the excludes array. None of the other suggestions worked.This should really not be closed, typescript should print an error telling you what’s going on. It’s near impossible to figure out currently. I’m not sure what causes it every so often, VSCode maybe updating tsconfig.json references accidentally, but it slows everything down a ton and each time has been non-trivial to debug.
Just had the same issue for
d.tsfiles. While everything was set correctly in terms oftsconfigdirs, I had this error because by accident my source code imported from thedistdir rather thansrc, so it was kinda circular I guess. Changing theimportfixed this.Hope someone will find this comment useful.
This solution worked like a charm for me! My
tsconfig.jsonlooks like this now:Similary, I hit the issue if you have an
index.tswith a bunch of lines likeexport * from './foo', and in one of those files, I was importing withimport foo from '.'rather thanimport foo from './foo'in one of those exported files.I banged my head on this for the last two days until I deleted
index.tsand had an import error. It was very non-obvious.I ran in to this as well when using an
outDirwhich was under my sources directory.How is it that the typescript compiler does not know by default that it should not try to compile things in the
outDir? That seems strange. Adding the outdir to the exclusion list did fix it though.Using a mono repo? Check for (implicit and unwanted/accidental) cyclic dependencies! That was what caused this issue for me. I wrote an answer here on StackOverlflow
If you have your
outDirunder your project root, and you are just including everything under the root, then this would be expected. Generally you want to have your build output somewhere other than within the project source folder.Same problem, I solve It by doing this :
Add in package.json
"files": [ "dist/**/*" // Your build repos ]and add in tsconfig.json
"include": [ "src", "index.ts", // put your files here ],thx, It works for me.
I needed to manually exclude my build
lib/directoryHad the same error message. Issue was that I had two files with the same name but different extension in the same folder so removing the one with
.jsextension fixed that.noEmitis a better solution if you do want to have generated*.jsfiles to be analyzed. They are not necessarily generated withtsc, after all.In my case, Ream.js generates
.ream/**.jswhich I then import withimport XXX from '#out/yyy'in my code (which works, but generates the warning “Cannot write file…” in VS Code).Basically, the only reason to have
"noEmit": falseis when you use rawtsc. For all other environments (ts-node/ts-node-dev, webpack, rollup) it’s safer to enable it.@mkermani144 thanks!
@Ghirigoro No, the problem wasn’t that I was using a package path, it’s that it was using a package path to import a package from within itself. Even without Typescript, that won’t work.
Basically what I had done was the equivalent of this:
If you then try to run that with node:
If you replace the package name then it works correctly:
I suspect that what happened was that when the build was run, all of the other packages that were importing from
@my-project/utilshad that import resolved topackages/utils(because they had the correct entries in thereferencesarray in tsconfig), so they built normally. But because that package wasn’t supposed to be importing itself that import got resolved tonode_modules/@my-project/utils, which was a symlink topackages/utils, but TypeScript didn’t detect that they were actually the same project so it tried to build it twice, but since both builds had the same output directory I ended up with this error.If excluding the
outDirdidn’t work for you, try checking to see if you happen to have duplicate files with the same path and filename but different extensions.We had a pre-defined ‘dist’ folder in our app. Deleting that fixed it for me.
Can you share your virtual project structure with me, to get that you need to:
Tools>Options>Text Editor>TypeScript>Project, and checkDisplay Virtual Projects when no Solution is loaded;TypeScript Virtual Project@Arkanic Deleting the folder is a reasonable workaround, but it has downsides. For example if you have an incremental build, or there’s other expensive steps required to prepare the build folder. It seems like the problem here is that TS doesn’t listen to you when you tell it to exclude something.
If tsc runs fine the first time then errors with this error every time after that it’s because it doesn’t want to overwrite the compiled files. Just delete the folder then run tsc again, and it’ll be fine.
I am dealing with the same issue and the problem is only happening if I have:
in my
package.json. Anyone has a clue why that is?Fixed this by adding
"types": "./{name of dist directory}/**/**.d.ts"to my package.json fileI experienced this in a project with project references. None of the excludes mattered, what did was the
typingsentry hinted at by @elmpp.So this will produce
TS5055: Cannot write file because it would overwrite input filein a project references monorepo:but this will report no errors:
So evidently I’ll either need to manipulate this prepublish, or publish the src in the package.
I also got this error when I had two files
foo.tsandfoo.tsx, both of which would compile tofoo.js, obviously.Seems that this issue related with this issue. Sample solution to reproduce - uploaded.
@mhegazy
right, it was fixed by https://github.com/wc-catalogue/blaze-elements/commit/cdb94bf8feb3a1ad7e21e6fce243e3322c1334cc
sry for delayed answer and thx 4 help!
We’re having a similar issue – same version of typescript (2.2.1) and Visual Studio 2015 (Update 3); the first time the build runs, there are zero errors, but after that we get hundreds of these errors.
It appears that all of the errors (for us) are in the “node_modules” folder, which we have set to exclude in our tsconfig.json file. – From looking at similar bugs, it seems like the excludes aren’t treated the same in this version of typescript?
Our tsconfig.json file:
Some of the errors we get (they’re all the same, but different files):
Again, if we delete the “node_modules” folder the build will work once, but then once it’s been recreated, it will fail on the next rebuild.