44

I am creating a new Angular project and would like all the Angular dependencies to be from the stable 7th version. However, it seems that while running the ng new app command the Angular always fetches the latest version. My package.json shows all the angular packages like core, animations etc from ~8.0.0.

I know that I can modify the package.json and set these dependencies to be fetched as per the semantic 7.x.x, but I would rather want it to be done automatically so that I do not run a chance of mis-matching any peer dependencies.

So, Is there a way that we can tell the CLI to fetch a particular angular version and all the dependencies according to that angular version.

EDIT: I have already tried insatlling @angular/cli@7.x.x. Even after doing this if I run ng new app, my depencies are fetched as per the 8th version

Saurabh Tiwari
  • 4,632
  • 9
  • 42
  • 82

5 Answers5

35

It can be done by using npx command that downloads and runs the package without installing it.

For example, npx @angular/cli@9 new my-project creates a new folder my-project in the current folder and puts a new project here using angular version 9. The local version of @angular/cli in this case will be the same as used in npx command so you can just continue working.

The syntax of the command is as follows npx @angular/cli@<package version> new <project-name>.

Shlang
  • 2,495
  • 16
  • 24
  • Here it does not work as expected. It seems like npx is picking up the version of the globally installed angular cli version. When removing the global angular cli (`npm uninstall -g @angular/cli`) then the npx command works fine and installs the specified version. – spierala Aug 04 '21 at 10:18
  • @spierala, If you don't specify the version you want by adding `@` after package name it picks the one you have installed globally. That is why I put `@9` in my answer. – Shlang Aug 04 '21 at 11:53
  • 1
    I tried again... it takes the global version in any case with the Windows Power Shell (in IntelliJ), but it works fine e.g. in the Git Bash. I will use Git Bash in the future :) – spierala Aug 05 '21 at 07:23
27

There is no way to tell Angular CLI the specific Angular version you want to install. Instead, you can switch to another version of the Angular CLI and then create Angular project.

Run these commands first:

npm uninstall -g @angular/cli
npm install -g @angular/cli@7.1.0

After it is installed, you can run:

ng new angular7

This will create your Angular 7 project with correct dependencies:

"@angular/animations": "~7.1.0",
"@angular/common": "~7.1.0",
"@angular/compiler": "~7.1.0",
"@angular/core": "~7.1.0",
"@angular/forms": "~7.1.0"
Developer Thing
  • 2,704
  • 3
  • 21
  • 26
  • 2
    Most people will need to run `npm uninstall -g @angular/cli` before doing the above. – Reactgular Jun 14 '19 at 13:18
  • As I mentioned in the answer, I have already tried this locally. However, I will try it again with -g and uninstalling and will update if it works for me. – Saurabh Tiwari Jun 15 '19 at 17:11
  • 2
    Worked when I uninstalled previous version. Thanks. However, I think its much better if the version parameters could be supplied with the `ng new` command. – Saurabh Tiwari Jun 15 '19 at 17:15
15

You can also install the angular CLI locally... lets say in /my-folder:

Run inside my-folder: npm i @angular/cli

This installs the latest available version of the CLI, but you can install whatever existing version (e.g. npm i @angular/cli@8.x)

When the npm install is done the angular CLI will land here: /my-folder/node_modules/@angular/cli

Inside my-folder you can run ng new my-project

This will create a new angular project here: /my-folder/my-project

The angular version of the new project will match the local CLI version.

Finally you should remove /my-folder/node_modules It is not needed anymore.

The ng commands will use by default the local angular CLI (in this case inside /my-folder/node_modules). If there is no local CLI then the global CLI will be used.

spierala
  • 2,349
  • 3
  • 25
  • 50
3

Try this:

ng new name-of-project --version=your_version

For Example to install version 7:

ng new name-of-project --version=7
1

I had the same issue and solve it as below:

  1. Create a new folder in your desired directory

  2. Then open the CMD (command prompt) inside the newly created folder. In windows, you can open the newly created folder and type cmd inside the address bar.

  3. Now Install your specific Angular CLI version like below:

    npm install @angular/cli@13.0.2 // Angular version 13.0.2
    npm install @angular/cli@latest // Angular latest version
    
  4. Now create an Angular new project with this command:

    ng new your_project_name
    

So, your current created project will be the same as the above CLI version

Kamran Taghaddos
  • 452
  • 1
  • 10
  • 22