89

My npm version is 5.5.1 and angular cli version is 6.2.1. When I try to create a new project using the command ng new Project_name then it is creating the latest version of Angular (in my case it is creating Angular version ^6.1.0), but I want Angular 4.

My question is how do I create a new Angular 2/4/5 project using a specific version, instead of the latest version?

I don't think changing the version value in package.json will help in my case because there are some differences in the older versions and the latest 6 version (like the name of one file has been changed from angular-cli.json to angular.json and not only name but content is also changed).

I've also raised the same question on the angular-cli GitHub site.

starball
  • 20,030
  • 7
  • 43
  • 238
Ravi
  • 1,614
  • 4
  • 14
  • 27
  • 1
    IMHO, you must install CLI that is aligned with Angular4, was published at that time. AFAIK there is no flag to create project with older version. – PeS Sep 14 '18 at 12:06
  • Possible duplicate of [Angular 6: How to install a previous version of Angular project](https://stackoverflow.com/questions/50272621/angular-6-how-to-install-a-previous-version-of-angular-project) – kboul Sep 14 '18 at 12:06

7 Answers7

141

You can use npx command which is Node.js package runner, by running packages directly from the registry without effecting the globally installed package registry (-g).

@next will automatically install the latest preview version from Angular repo (now days refers to version 9 rc), like so:

npx @angular/cli@next new Angular9Project

Otherwise, you can provide the specific version:

npx @angular/cli@7 new Angular7Project

NPX comes bundled with NPM version 5.2+

EladTal
  • 2,167
  • 1
  • 18
  • 10
117

Using CLI you can not create specific angular version.

But you can install specific version of angular CLI into particular folder.

For Example :

First create new folder inside any drive. I'm going to create demo folder in D drive. Ex: d:\projects\demo.

Then find this folder inside Command Prompt(cmd) or just type cmd into your created folder addressbar in windows and hit enter.

Now type angular specific version command : npm install @angular/cli@1.7.x for angular 5. and use similar command for other version.

After complete the installation, just create new angular project into your specific folder that you recently install angular. Ex: d:\projects\demo\.

Now create angular project using the command ng new Project_name and it will create your specific angular version Project.

In my example it will create angular 5 project.

Shashikant Devani
  • 2,298
  • 1
  • 12
  • 25
  • 5
    Instead of creating the project inside the demo/ folder, use `ng new Project_name --directory ./` to create that project in the current directory. – Serzhan Akhmetov Mar 15 '20 at 18:25
  • This wasn't working form me. Last step fail: 'ng' is not recognized as an internal or external command, operable program or batch file. – Lukas Coorek Dec 16 '21 at 09:35
  • 2
    I still get a "A merge conflicted on path package.json" because I don't want to have 2 different node-modules folders, My global is v12 and I'd like to install v11, what can I do? @SerzhanAkhmetov – Yogurtu Dec 22 '21 at 21:20
  • Delete package.json file then run command: `node ./node_modules/@angular/cli/bin/ng new Project_name --directory ./` – IZI Jul 15 '23 at 18:07
35

The Easy Way: example:npm install @angular/cli@6 here the -g flag tells npm to do this install globally. The 6 on the end tells npm that I want the latest available version 6.

if I want to create a new application for Angular 5 I do this :

> npm install @angular/cli@1
> ng new my-ng5-app

Remember, Angular CLI was version 1.7 for Angular 5.

if I want to create a new application for Angular 6 I do this :

> npm install @angular/cli@6
> ng new my-ng6-app

if I want to create a new application for Angular 7 I do this :

> npm install @angular/cli@7
> ng new my-ng7-app

if I want to create a new application for Angular 12 I do this :

> npm install @angular/cli@12
> ng new my-app-name

I hope it would be helpful for you thanks....!

Bhavesh Ajani
  • 991
  • 9
  • 11
  • 7
    Great answer, it helped me. But why are you installing the "cli" globally? It worked for me with "npm i @angular/cli@7", so it installs the version 7 on the folder and keeps my global cli with the version 8 – Bernardao Aug 01 '19 at 16:58
  • 8
    Won't -g flag effect the other projects? – Charlie Jun 11 '20 at 06:41
  • this answer has been edited to remove the -g flag. so it won't work because the global installation of angular-cli is used to create new projects. the -g flag won't affect other projects because each project has a local installation of angular-cli – Xel May 14 '23 at 11:29
  • This didn't work for me. The angular still created angular version 16, which was installed globally using -g. What I wanted was angular version 11. – Agung Sudrajat Aug 15 '23 at 12:42
  • install without using the '-g' flag – Bhavesh Ajani Aug 15 '23 at 12:53
5

TL;DR
Use a package called npx (run npm i -g npx if not already installed) and when you need to create an angular project, just use this command the very first time:
npx -p @angular/cli@latest ng new hello-world-project

Note: Replace @latest with your desired CLI version.
Remember:, For Angular 6 & above, the Angular-CLI version has been brought to the same level as the Angular such that npx -p @angular/cli@6 creates angular 6 project & npx -p @angular/cli@7 creates angular 7 project etc.

Explanation:
So For example if you want to create angular 4 project, modify the above command to include the angular-cli version 1.4.10 like this npx -p @angular/cli@1.4.10 ng new hello-world-project and then when your project setup is complete you can go back to using the normal ng generate and other commands.

Angular-cli versions indicate which angular version will be associated with a project & angular-cli 1.4.10 creates angular 4 projects

Edits:

Here is some useful versioning info about which cli creates which angular version.

 CLI version     Angular version

 1.0 - 1.4.x       ^4.0.0
 1.5.x             ^5.0.0
 1.6.x - 1.7.x     ^5.2.0
 6.x               ^6.0.0
 7.x               ^7.0.0

Also, if you want to use latest stable version to create a certain angular project you can just use npx command like this npx -p @angular/cli@1.7 and it will use cli version 1.7.4 which is the most latest stable version for angular 5.

Junaid
  • 4,682
  • 1
  • 34
  • 40
3

I think the easiest and flexible solution is to use npx:

npm install -g npx

for example for create a new angular 11 project:

npx -p @angular/cli@11.1.2 ng new Angular11App
pixparker
  • 2,903
  • 26
  • 23
2

Create a package.json file then define the angular version you want to install then run npm install it will create project in the required version irrespective of the global angular cli

Exterminator
  • 1,221
  • 7
  • 14
  • this is great solution if you want to downgrade a project, in my case from 14 (not lts yet) to v13-lts , edit you existing package.json file, change all version numbers to "^13.0.0" and remember to match all dependencies version when you try to install with these commands `$rm -rf node_modules`, `$rm pacakge-lock.json`; `$npm install` – Timy Shark Jun 11 '22 at 17:12
  • to install specific version of angular cli `$npm uninstall --location=global @angular/cli`, `$npm install --location=global @angular/cli@v13-lts` for example. – Timy Shark Jun 11 '22 at 17:18
2

this should work Use a package called npx npm i -g npx and when you need to create an angular project use this command the first time npx -p @angular/cli ng new hello-world-project after that you can use normal commands to work like ng g c abc

Jaideep
  • 908
  • 8
  • 22