Typescript error “Cannot write file … because it would overwrite input file.”

后端 未结 19 1246
醉梦人生
醉梦人生 2021-02-01 12:04

In my Typescript 2.2.1 project in Visual Studio 2015 Update 3, I am getting hundreds of errors in the error list like:

Cannot write file \'C:/{{my-project

相关标签:
19条回答
  • 2021-02-01 12:17

    I had the same issue. In my case it was caused, because I had two files with the same name in one module: index.ts index.tsx.

    I renamed one of them and the problem got fixed.

    0 讨论(0)
  • 2021-02-01 12:18

    Set outDir.

    "outDir": "./",
    

    this hint is that if you don't set outDir, then the output will be placed directly next to the input file. After allowJs, the JavaScript file will also be compiled. Then, the compiled JavaScript file will overwrite your source file. It’s just reminding you of this.

    0 讨论(0)
  • 2021-02-01 12:18

    I solved this by removing "declaration": true from my tsconfig.json file. Though now I don't have declarations anymore so that didn't help.

    0 讨论(0)
  • 2021-02-01 12:21

    In my instance, I was using the outDir option but not excluding the destination directory from the inputs:

    // Bad
    {
        "compileOnSave": true,
        "compilerOptions": {
            "outDir": "./built",
            "allowJs": true,
            "target": "es5",
            "allowUnreachableCode": false,
            "noImplicitReturns": true,
            "noImplicitAny": true,
            "typeRoots": [ "./typings" ],
            "outFile": "./built/combined.js"
        },
        "include": [
            "./**/*"
        ],
        "exclude": [
            "./plugins/**/*",
            "./typings/**/*"
        ]
    }
    

    All we have to do is exclude the files in the outDir:

    // Good
    {
        "compileOnSave": true,
        "compilerOptions": {
            "outDir": "./built",
            "allowJs": true,
            "target": "es5",
            "allowUnreachableCode": false,
            "noImplicitReturns": true,
            "noImplicitAny": true,
            "typeRoots": [ "./typings" ],
            "outFile": "./built/combined.js"
        },
        "include": [
            "./**/*"
        ],
        "exclude": [
            "./plugins/**/*",
            "./typings/**/*",
            "./built/**/*" // This is what fixed it!
        ]
    }
    
    0 讨论(0)
  • 2021-02-01 12:24

    In my case due to developing a library and an app at the same time...

    Moving a file, which has an import from the library, from the app to the library, resulted that the file that is now in the library would import stuff from its own dist folder..

    Funny thing is.. this is actually refactoring at its best.. it kept the correct reference to the file :)

    0 讨论(0)
  • 2021-02-01 12:25

    set allowJS: true is not woI set outDir : true, it's work

    0 讨论(0)
提交回复
热议问题