typescript 打字稿错误“无法写入文件......因为它会覆盖输入文件。”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42609768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:36:30  来源:igfitidea点击:

Typescript error "Cannot write file ... because it would overwrite input file."

visual-studiotypescriptvisual-studio-2015typescript2.0

提问by CBarr

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

在 Visual Studio 2015 Update 3 中的 Typescript 2.2.1 项目中,我在错误列表中收到数百个错误,例如:

Cannot write file 'C:/{{my-project}}/node_modules/buffer-shims/index.js' because it would overwrite input file.

无法写入文件 'C:/{{my-project}}/node_modules/buffer-shims/index.js' 因为它会覆盖输入文件。

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.

它看起来总是这样。它实际上并没有阻止构建,并且一切正常,但是错误列表会分散注意力,并且在发生错误时很难找到“真正的”错误。

visual studio error list

Visual Studio 错误列表

Here is my tsconfig.jsonfile

这是我的tsconfig.json文件

{
  "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?

我怎样才能摆脱所有这些错误?

采纳答案by CBarr

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

通过更新到 Typescript 2.3.x 似乎为我解决了这个问题

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

此外,使用 Visual Studio 2017 也是一个很大的改进。不过,我强烈建议您进行两项更新。

回答by Vince Horst

In my instance, I was using the outFileoption but not excluding the destination directory from the inputs.

在我的例子中,我使用了outFile选项,但没有从输入中排除目标目录。

// 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 goodies in the outDir:

我们所要做的就是排除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!
    ]
}

回答by Donniewiko

I got the same issue. In my case it was the result of the option: allowJs: true.

我遇到了同样的问题。就我而言,这是选项的结果:allowJs: true.

So i basicly had to remove that line to get rid of the errors. I do not see it in your code, but perhaps it helps you here.

所以我基本上不得不删除那条线来消除错误。我没有在你的代码中看到它,但也许它在这里对你有帮助。

Good luck!

祝你好运!

回答by Jai

There's several possible causes to this.

这有几个可能的原因。

  • In your tsconfig.json:
    • Set outDirto "dist" or the name of another same-level folder. (prefixing with './' is unnecessary). This is where the build files go.
    • Set allowJsto falseor delete the line. Note: enabled, allowJswill 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 mainto "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.
  • 在您的 tsconfig.json 中:
    • 设置outDir为“dist”或另一个同级文件夹的名称。(不需要前缀“./”)。这是构建文件所在的位置。
    • 设置allowJsfalse或删除该行。注意:启用后,allowJs将与声明设置/标志冲突。默认情况下不启用它。
    • exclude.
  • 在你的 package.json 中:
    • 设置main为“index”或其他选定的名称。不要使用构建文件夹(例如“dist/index”)作为前缀,也不要使用不必要的“./”。
    • types(现代别名typings)设置为“索引”。添加扩展名(.d.ts 或 .js)是不必要的。

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.jsonand package.jsonfiles 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.

虽然你可以用一百万种不同的方式来实现它,但为了简单起见并加深理解,最好首先坚持通用实践——比如在树的同一级别使用“dist”、简单的tsconfig.jsonpackage.json文件, 等等。当然,翻阅你的 node_modules 文件也会加深你的理解,但生活中还有更多有意义的事情。

回答by jcampbell1

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

由于 VSCode 自动完成文件dist/夹中的文件,我遇到了这个问题。

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

To solve the problem just fix the import:

要解决此问题,只需修复导入:

import { SomeClass } from './someclass' 

回答by Mapad

Adding 'dist' to the excluded directories in tsconfig.json worked for me:

将“dist”添加到 tsconfig.json 中排除的目录对我有用:

{
  "exclude": ["node_modules", "dist"]
}

回答by Cheney Shi

Set outDir.

设置 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.

这个提示是,如果你不设置outDir,那么输出将直接放在输入文件的旁边。在allowJs之后,JavaScript文件也会被编译。然后,编译后的 JavaScript 文件将覆盖您的源文件。它只是提醒你这一点。

回答by Eric Pitcher

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.

当我收到此错误时,将 "outDir": "./dist" 添加到 tsconfig.json 中的 compilerOptions 对我有用。我很确定这只是输出此错误的 Visual Studio Code TypeScript 扩展。我将 ts-loader 与 Webpack 一起使用,而不是直接使用 tsc 编译器,因此我不必指定 outDir,因为 webpack 配置控制了它,但是如果这让 VS Code 扩展感到高兴,那就太好了。

回答by Kalesh Kaladharan

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

我有同样的问题,这是因为exclude选项。如果指定exclude选项,则还必须添加输出目录。

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

我有这样的排除选项"exclude": ["resources/js/**/*", "node_modules"],输出目录设置为outDir:"./build".

Adding buildto the exclude option fixed the issue.

添加build到排除选项解决了这个问题。

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

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

回答by Muhammed Moussa

this config works for me

这个配置对我有用

"allowJs": true