How to install Angular-CLI with Angular v4.x

2020-02-01 02:19发布

问题:

I've installed Angular-CLI using this command

npm install -g @angular/cli

but I've realized that it has installed Angular v5.1.3 but I need to use Angular 4.x.

How can I install the last version of Angular-CLI with the last version of Angular 4.x?

回答1:

I think downgrade your cli is not good idea becuase they are fixing lots of bug. You can specific your angular in package.json file. don't depend on your cli.

Edit

First install latest version from your cli. Then Specify the version you want in the 'dependencies' section of your package.json. For example if you want Angular 4.3.4 then you can edit you package.json file like

"dependencies": {
    "@angular/animations": "^4.3.4",
    "@angular/common": "^4.3.4",
    "@angular/compiler": "^4.3.4",
    "@angular/core": "^4.3.4",
    "@angular/forms": "^4.3.4",
    "@angular/http": "^4.3.4",
    "@angular/platform-browser": "^4.3.4",
    "@angular/platform-browser-dynamic": "^4.3.4",
    "@angular/router": "^4.3.4",
     ...........................
     ...........................
    }

Also change your devDependencies

    "devDependencies": {
      "@angular/compiler-cli": "^4.3.4",
      "@angular/language-service": "^4.3.4",
      .............
    }

now run npm install

  npm install

After finishing install you should see your version by ng -v command like this

Angular CLI: 1.6.3
Node: 9.3.0
OS: linux x64
Angular: 4.4.6
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router, tsc-wrapped

@angular/cli: 1.6.3
@angular-devkit/build-optimizer: 0.0.38
@angular-devkit/core: 0.0.25
@angular-devkit/schematics: 0.0.48
@ngtools/json-schema: 1.1.0
@ngtools/webpack: 1.9.3
@schematics/angular: 0.1.13
@schematics/schematics: 0.0.13
typescript: 2.4.2
webpack: 3.10.0


回答2:

you can use

 npm i @angular/cli@1.4 -g

if you want package JSON file with the versions lower than 5.

Starting with v1.5 of the Angular CLI, we have added support for Angular v5.0.0 and will generate v5 projects by default. see link



回答3:

You can use a simple hack: locally install @angular/cli@1.4 and use it to generate a new project.

To do that, follow these steps:

  1. In a new folder create a simple nodejs project with the command npm init
  2. Locally install angular-cli 1.4 under this project: npm install @angular/cli@1.4
  3. Delete the node project files except the newly created node_modules folders: this means that you will need to delete package.json and any other that it has may be created like karma.js etc.
  4. Now you can create a new Angular 4 project with the command ng new <project name>. It will use the local version of angular-cli previously installed.
  5. You may now remove the node_modules folder.

Then, if you cd in your new project you will have a fully Angular 4 project.



回答4:

You can use

npm install -g @angular/cli@1.5

This should install Angular 4.4.6 and will use angular-cli 1.5.5

This was the latest version before 5 came in



回答5:

This means you installed @angular/cli globally on your computer so that you can use the ng commands from anywhere on a console.

You need to create a new Angular Project using ng new my-project-name which will generate all the basic files you need for an Angular project.

In these files, you will find a package.json file that contains all the library references you need in your project. It should have the angular package by default at version 5+.

To be 4.X.X like you want, you need to find the best @angular/cli version compatible with the Angular version you want (I advise using 1.5.0 or less for @angular/cli).



回答6:

Using npm install -g @angular/cli you will install the lastest stable version of the @angular/cli. So once you create an angular project using the cli there will be a package.json file, to specify the version of angular to use for the project change the version you would like to use in the package.json file. Then run npm install.