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

后端 未结 19 1331
醉梦人生
醉梦人生 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:26

    It seems like this issue was fixed for me by updating to Typescript 2.3.x

    Also, using Visual Studio 2017 was a big improvement as well. I highly recommend that you make both of these updates though.

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

    There's several possible causes to this.

    • In your tsconfig.json:
      • Set outDir to "dist" or the name of another same-level folder. (prefixing with './' is unnecessary). This is where the build files go.
      • Set allowJs to false or delete the line. Note: enabled, allowJs will conflict with the declaration setting/flag. It isn't enabled by default.
      • Include "dist" (or your build folder) in exclude.
    • In your package.json:
      • Set main to "index" or some other chosen name. Don't prefix with the build folder (eg "dist/index"), nor the unnecessary "./".
      • Set types (modern alias of typings) to "index". Adding the extensions (.d.ts or .js) is unnecessary.

    Though you can have it a million different ways, for the sake of simplicity and developing understanding it's best to stick to common practices at first - like using "dist", simple tsconfig.json and package.json files at the same level in the tree, and so on. Of course, rooting through the files of your node_modules would also deepen your grasp, but there are more rewarding things in life.

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

    I have run into this issue due to VSCode autocompleting a file in the dist/ folder.

    import { SomeClass } from '../../dist/xxx/someclass' 
    

    To solve the problem just fix the import:

    import { SomeClass } from './someclass' 
    
    0 讨论(0)
  • 2021-02-01 12:30

    I had the same issue and it's because of the exclude option. If you specify exclude option, you have to add the output directory also.

    I had the exclude option like this "exclude": ["resources/js/**/*", "node_modules"] and the output directory set as outDir:"./build".

    Adding build to the exclude option fixed the issue.

    exclude:["resources/js/**/*", "node_modules", "build"]

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

    Because the problem can have a wide set of reasons! I'm going to share my experience on when i encountered the error !

    A story

    In my case! I had two files ! One src/index.ts the other on src/test/logToFile.directTest.ts.

    excluding src/test/logToFile.directTest.ts solved the problem! It seems each resolution was trying to write to the same file!

    My config for the declaration was:

    {
      "compilerOptions": {
        "declaration": true,
        "declarationDir": "./dist",
        "module": "commonjs",
        "noImplicitAny": true,
        "lib": ["ESNext", "DOM"],
        "outDir": "./dist",
        "target": "es6",
        "moduleResolution": "node",
        "resolveJsonModule": true,
        "esModuleInterop": true
      },
      "include": ["src/**/*"],
      "exclude": ["node_modules", "dist"]
    }
    
    

    And everything was setup correctly! You can notice that i excluded the dist directory. And correctly set the outDir which are necessary (you can get the error if you don't! And all the other answer mentioned that).

    More then that i was using the config with other repositories and packages! And i haven't any problems!

    At the end ! it was this:

    import { Logger } from '../..';
    

    My import was wrong!

    It should have been:

    import { Logger } from '..';
    

    Porting the module to it's own repository i forget to change the import!

    And i included back the file! And tested and all worked well!

    And to bring the benefit from the story!

    If All the making sense solutions (or things to do) are respected and you still get the problem! Make sure to check your imports within the ts files!

    An import can refer to root (for example) and automatically redirect back to dist! And so the .d.ts file! Typescript should normally throw an error! As that stand out of the directory folder! But it didn't! And made that error!

    And to bring more benefit

    Explaining the error

    In short it's described here:

    https://github.com/microsoft/TypeScript/issues/6046#issuecomment-210186647

    in short an input file is either a file that you pass on the command line, or that is a dependent of one of the files you pass on the command line transitively.

    If two maps to the same declaration file! And that can happen when they share the same name! Or refer back to the declaration file itself! Imported from a file! Through a bad import (like in my case)! Or that dist is not ignored!

    Looking for imports is a thing to check! If you excluded the dist directory and set up the outDir correctly

    Very interesting to know you can check the inputs files through this command:

    tsc --listFiles
    

    When the problem reside you'll find the declaration file within the list!

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

    Adding "outDir": "./dist" to compilerOptions in tsconfig.json worked for me when I got this error. I'm pretty sure this is only the Visual Studio Code TypeScript extension outputting this error. I use the ts-loader with Webpack and not the tsc compiler directly so I didn't have to specify the outDir because the webpack config controls that but if this makes the VS Code extension happy it's good.

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