错误:TSError:⨯无法编译TypeScript

45

我遇到了问题:

我的项目使用Angular4和typescript构建,进行端到端测试使用protractor和karma。

Travis-ci出现以下错误:

[03:34:54] E/launcher - Error: TSError: ⨯ Unable to compile TypeScript
Cannot find type definition file for 'jasmine'. (2688)
Cannot find type definition file for 'node'. (2688)
e2e/app.e2e-spec.ts (1,32): Cannot find module './app.po'. (2307)
e2e/app.e2e-spec.ts (4,1): Cannot find name 'describe'. (2304)
e2e/app.e2e-spec.ts (7,3): Cannot find name 'beforeEach'. (2304)
e2e/app.e2e-spec.ts (11,3): Cannot find name 'it'. (2304)
e2e/app.e2e-spec.ts (13,5): Cannot find name 'expect'. (2304)

The command "ng e2e" exited with 4.

我的 tsconfig.json:

{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "baseUrl": ".",
    "paths": {
      "*": ["./node_modules/*", "*"]
    },
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "types": [ "node", "jasmine" ],
    "typeRoots": [
      "./node_modules/@types"
    ],
    "lib": [
      "es2016",
      "dom"
    ]
  }
}

我的 tsconfig.spec.json 文件:

{
  "compilerOptions": {
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [
      "es2016",
      "dom"
    ],
    "outDir": "../out-tsc/spec",
    "module": "commonjs",
    "target": "es5",
    "baseUrl": "",
    "types": [
      "jasmine",
      "node"
    ],
    "typeRoots": [
      "../node_modules/@types"
    ]
  },
  "files": [
    "test.ts"
  ],
  "include": [
    "**/*.spec.ts"
  ]
}

Node v6.10.3

npm v3.10.10

请帮帮我。


你可以参考这里提供的解决方法(https://github.com/AngularClass/angular-starter/issues/615)。不确定是否完全相同。干杯! - demouser123
我更新了我的devDependencies并修复了这个错误! - RicoLiu
它对我有效。https://dev59.com/P836oIgBc1ULPQZF-5nM%e2%a8%af-unable-to-compile-typescript - saman samadi
8个回答

66

我曾经遇到过相同的问题。首先,我从package.json中删除了ts-nodetypescript。然后,

npm install ts-node --save-dev
npm install typescript -g 
npm install typescript --save-dev

8
对我来说,只需使用“npm i ts-node@latest”更新ts-node即可解决问题。谢谢。 - ya_dimon
1
FYI:这些命令会更新 TypeScript 和 ts-node 版本。 - Janac Meena
谢谢,对我来说在React + Express的起始项目中使用Vita可以正常工作。 - Rafael
还需要确保当前版本的Angular支持安装typescript的版本。在我的情况下,我使用的是Angular 11,安装了typescript 4.9.3,因此Angular发出了警告,我不得不降级到4.1.5,这是Angular 11所支持的最大版本(当时)。 - Renat Gatin
1
只需在文件夹中添加tsconfig.json文件,然后运行"tsc --init"命令。在运行上述命令之后。 - undefined

7
我来到这里寻找一个解决方案,因为当我更新我的 typescript 版本时出现了类似的错误。 我正在运行一个 Angular2 项目,版本号为 "@angular/cli": "1.2.0",而不是新项目。 我将 typescript 版本从2.0.3更新到2.7.2,然后在运行ng e2e时遇到以下类似的问题:

Error: TSError: ⨯ Unable to compile TypeScript
Cannot find type definition file for 'core-js'. (2688)
Cannot find type definition file for 'grecaptcha'. (2688)
Cannot find type definition file for 'jasmine'. (2688)
Cannot find type definition file for 'moment-timezone'. (2688)
Cannot find type definition file for 'node'. (2688)
Cannot find type definition file for 'q'. (2688)

这篇文章中提供的解决方案对我没有用。

我通过将ts-node 更新到5.0.1 来解决它。即使我在tsconfig.json中设置了experimentalDecoratorstrue,但是版本高于此版本的ts-node 在运行 ng e2e 时会产生如下错误:

error TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option to remove this warning

除了上述更改外,我还更改了protractor.conf.js中的几行代码。

更改前:

beforeLaunch: function () {
    require('ts-node').register({
        project: 'e2e'
    });
},

to

beforeLaunch: function () {
   require('ts-node/register')
}

这篇文章是给那些读者准备的,如果他们遇到了类似的问题,而之前提出的解决方案对他们无效。

在我的情况下,问题是一个spec.ts文件中缺少了闭合的花括号。 - Mr_Green
更新 ts-node 并更改 beforeLaunch 方法对我很有帮助。我正在处理 Angular 10。 - JB17

1

确保 ts-node 不会解释 js 文件。例如,如果您使用 knex 数据库迁移,并且将 knex 指向构建目录中的 js 迁移文件,则 ts-node 将抛出错误,因为它尝试解释 js 文件。


我不是很清楚,但是这个stackoverflow问答帮助我解决了我的问题。我只是在谷歌上搜索“ts-node无法解释js文件”,然后找到了那个问答。谢谢。 - Kasir Barati

1
在我的情况下,我只需删除nodes_modules目录,然后运行npm install命令,就可以解决问题了。

1
你的答案可以通过增加支持信息来改进。请点击[编辑]以添加更多详细信息,例如引用或文档,以便他人确认你的答案是否正确。您可以在帮助中心找到有关编写好答案的更多信息。 - Community

1
对我来说,解决方案是在tsconfig.json文件中将"jsx": "react-jsx"更改为"jsx": "react"

2
我完全没有tsconfig.json文件,一旦我添加了它,问题就解决了。 - ttemple

0

我也遇到了同样的问题,错误已经附在下面了 enter image description here

解决方案:检查tsconfig.json并设置"allowJs": false


-1

如果只是 npm install -g typescript 无法工作。

尝试在项目文件夹中执行 tsc


这是关于什么的?这是解决方案吗? - Hiren Patel
我一直在多个Angular项目中工作,其中一些使用旧的TypeScript 3.3版本,因此为了使TypeScript正常工作,我们需要运行“tsc”。 - Deyvison Souto

-4
这是我针对此问题的解决方案: - 运行此命令
npm install -save-dev typescript@2.3.4
npm WARN code_gui@0.0.1 No repository field.
npm WARN code_gui@0.0.1 No license field.

+ typescript@2.3.4
updated 1 package and audited 657 packages in 2.539s
found 0 vulnerabilities

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接