The Angular Compiler requires TypeScript >=3.4.0 a

2020-01-24 05:23发布

问题:

I'm getting the following error when I do npm run build:

The Angular Compiler requires TypeScript >=3.4.0 and <3.5.0 but 3.5.3 was found instead.

I've tried following things separately:

npm install typescript@">=3.4.0 <3.5.0". Then deleted node_modules and package.json. Run npm install

npm update --force. Then deleted node_modules and package.json. Run npm install

I'm still getting the error:

My package.json contains following dependencies:

  "dependencies": {
    "@angular/animations": "8.1.0",
    "@angular/cdk": "^8.0.2",
    "@angular/cli": "^8.1.0",
    "@angular/common": "8.1.0",
    "@angular/compiler": "8.1.0",
    "@angular/core": "8.1.0",
    "@angular/forms": "8.1.0",
    "@angular/http": "7.2.15",
    "@angular/material": "^8.0.2",
    "@angular/platform-browser": "8.1.0",
    "@angular/platform-browser-dynamic": "8.1.0",
    "@angular/router": "8.1.0",
    "@ngx-translate/core": "^11.0.1",
    "@ngx-translate/http-loader": "^4.0.0",
    "angular-user-idle": "^2.1.2",
    "angular2-cookie": "^1.2.6",
    "core-js": "^2.6.7",
    "rxjs": "6.5.2",
    "rxjs-tslint": "^0.1.5",
    "stream": "0.0.2",
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^0.801.0",
    "@angular/compiler-cli": "8.1.0",
    "@angular/language-service": "8.1.0",
    "@types/jasmine": "~3.3.13",
    "@types/jasminewd2": "~2.0.6",
    "@types/node": "~12.6.1",
    "jasmine-core": "~3.4.0",
    "jasmine-spec-reporter": "~4.2.1",
    "protractor": "~5.4.2",
    "ts-node": "~8.3.0",
    "tslint": "~5.18.0",
    "typescript": "^3.4.5"
  }

ng --version gives following output:

Angular CLI: 8.1.2
Node: 10.16.0
OS: win32 x64
Angular: 8.1.0
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.801.2
@angular-devkit/build-angular     0.801.2
@angular-devkit/build-optimizer   0.801.2
@angular-devkit/build-webpack     0.801.2
@angular-devkit/core              8.1.2
@angular-devkit/schematics        8.1.2
@angular/cdk                      8.1.1
@angular/cli                      8.1.2
@angular/http                     7.2.15
@angular/material                 8.1.1
@ngtools/webpack                  8.1.2
@schematics/angular               8.1.2
@schematics/update                0.801.2
rxjs                              6.5.2
typescript                        3.5.3
webpack                           4.35.2

What could be going wrong here?

回答1:

Original Answer - Applicable for Angular 8

It seems like you have the latest version (v3.5.3) of TypeScript installed. You should be installing TypeScript v3.4.5 instead, which is the version that is supported by Angular 8.

You can try this command to install the specific version of TypeScript, rather than the latest version.

npm i --save-dev typescript@3.4.5

In addition, you might want to consider changing the caret ^ for the TypeScript version on your package.json:

  • Removing it completely means it will prevent npm from installing/using the most recent minor version (3.5.3).

    "typescript": "3.4.5"

  • Changing it to "~" would tell npm that any patch release is acceptable

    "typescript": "~3.4.5" // will use latest version that matches 3.4.X

For more info about the use of the npm packages versioning: https://docs.npmjs.com/about-semantic-versioning


Edited - Applicable to those who are working on other Angular versions such as Angular 9, or Angular 2 to 7)

|-----------------|--------------------|
| Angular version | TypeScript version |
|-----------------|--------------------|
|       6.1.x     |       3.6.x        |
|-----------------|--------------------|
|       7.2.x     |       2.9.x        |
|-----------------|--------------------|                                 
|       9.x       |       3.6.x        |        
|-----------------|--------------------|

Likewise, if we are using any other Angular versions, you may install the respective TypeScript versions.

npm i --save-dev typescript@3.6.4

Source - Refer to the full list of versions.


Additional useful information

There is actually a way to install TypeScript versions that are 'not supported' by the Angular version that is installed within your project, as pointed out by felipesilva. One benefit of doing so would be to allow you to install the latests versions of TypeScript (such as TypeScript 3.7.4 or 3.8.0-dev) on your project, thus unlocking features such as Optional Chaining and Nullish Coalescing, which has been available in TypeScript 3.7.x.

On your tsconfig.json, you can simply set disableTypeScriptVersionCheck as true.

{
  // ...
  "angularCompilerOptions": {
    // ...
    "disableTypeScriptVersionCheck": true
  }
}

However, do note that doing so is not recommended by the official Angular documentation:

Not recommended, as unsupported versions of TypeScript might have undefined behavior.



回答2:

Please uninstall typescript by using the following command:

npm uninstall typescript

and then install typescript specific version:

npm i --save-dev typescript@3.4.5


回答3:

Run this:

npm i --save-dev typescript@~3.4.5


回答4:

Your wrong is "^" "typescript": "^3.4.0" in your package.json

the solution is "typescript": "~3.4.0"



回答5:

This issue is mainly depended on @angular/cli version so typescript is depend on angular version

when you update angular check this website(devDependency)

https://david-dm.org/angular/angular-cli

and do

npm i --save-dev typescript@3.4.5

3.4.5version is required



回答6:

Run this script to find exact version

npm install typescript@">=3.4.0 and <3.5.0" --save