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

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

它一直都是这样的。它实际上并没有阻止构建,并且一切都可以正常工作,但是错误列表会分散注意力,并且很难在发生“真正的”错误时定位它们。

这是我的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"]
}

我怎样才能消除这些错误呢?


当前回答

当您尝试运行一个带有两个节点的项目时,很可能会发生这种情况。 对于这种假设,您可以在运行构建后测试名为“node”的计算机上的进程数。 我是怎么解决这个问题的:

步骤1。

比较

node -v

and

nvm -ls

,当前正在使用的版本。 在终端集中当前节点版本:

nvm use {neededVersion}

原则上,在nvm中删除节点的不必要版本(这将帮助IDE自动确定节点的正常版本)。

步骤2。

确定IDE中的当前节点。例如在WebStorm中: Node.js和NPM | Typescript:节点解释器-设置所需的版本。

其他回答

这有几个可能的原因。

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.

虽然你可以有一百万种不同的方法,但是为了简单和加深理解,最好一开始坚持常用的实践——比如使用“dist”,简单的tsconfig。Json和包。Json文件在树的同一级别,以此类推。当然,查找node_modules的文件也会加深您的理解,但生活中还有更有意义的事情。

我也有同样的问题。在我的例子中,这是选项:allowJs: true的结果。

所以我基本上要去掉这一行来消除错误。 我在您的代码中没有看到它,但它可能在这里对您有帮助。

我遇到过这个问题,因为VSCode自动完成dist/文件夹中的文件。

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

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

import { SomeClass } from './someclass' 

我通过从我的tsconfig中删除“declaration”:true来解决这个问题。json文件。不过现在我没有声明了,所以没用。

我也遇到了错误:无法写入文件'xx\xxx.js',因为它会覆盖输入文件。

我发现:

如果未指定outDir, .js文件将在生成它们的.ts文件的同一目录中生成。

为了避免意外覆盖源文件,编译器将显示此错误。

您需要指定outDir,或者设置noEmit: true,以避免生成类似JavaScript源代码的编译器输出文件。