How to install Angular CLI locally (without the -g flag)
Solution 1
As some of the comments suggest, you can have a local and global version of angular cli on your system.
To be able to access your local version instead of global (lets say you have a different version installed locally then your global install) use npm run-script ng
for example
npm run-script ng generate component SomeCoolComponent
check out this answer on github to a similar question: https://github.com/angular/angular-cli/issues/5955#issuecomment-320273493
Solution 2
TL;DR
Use a package called npx (runnpm 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 ng new hello-world-project
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/[email protected] 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/[email protected]
and it will use cli version 1.7.4 which is the most latest stable version for angular 5.
Check out this SO answer here where some other devs are trying to unfold this mystery.
Solution 3
Short Answer / TLTR
Start your project by specifying the package -p @angular/cli
, so node can find the program:
npx -p @angular/cli ng new <project-name>
Long Answer
The npm ecosystem has been moving more and more towards installing tools as project-local devDependencies
, instead of requiring users to install them globally. This is considered a good practice. As it allows to use multiple versions (one per project), instead of having one unique global version.
In order to start the project from scratch, you need to point to the package with -p
flag (otherwise npx
will not find it):
npx -p @angular/cli ng new <project-name>
- npx
npx
is a command that is installed together with node
and npm
, starting version 5.2 (July 2017). Most probably you already have it installed.
npx
allows you to run that npm command without having it installed locally. npx
will look for the latest version of the specified package (in this case @angular/cli
) and run the command ng
from the bin folder.
- specific versions
You could also install a specific version of Angular CLI. For example, let's say we need to install version 9.1. We could run:
npx -p @angular/[email protected] ng new <project-name>
- once it is installed
After the Angular CLI installs the project, go to the folder and use the npx ng
directly. For example:
npx ng serve
This will search inside the node_modules/.bin/
folder for the ng
command, which is a soft link pointing to ../@angular/cli/bin/ng
, the locally installed ng
command.
Links
Solution 4
To install angular locally:
npm init -y
npm i @angular/cli
npx ng new app-name
To update a locally installed angular version, say bump 8.x to 9.x, you can use
npx ng update @angular/core@9 @angular/cli@9
Solution 5
Just Follow this command
npm install @angular/cli
It worked for me.
Gil Epshtain
Web and Mobile Developer, Expert in the Client Side. I’ve been developing websites all the way back to the dark ages of Web1.0, Netscape, and Internet Explorer 1.x. Already back then I fell in love with the Internet and its ecosystem. Since then I have finished my academic degree and I have become a professional web developer. Adding to this my passion for designing and creative thinking will make me the perfect choice for your next big web project.
Updated on July 09, 2022Comments
-
Gil Epshtain almost 2 years
I've just started working with Angular and with Angular-CLI and I've seen that, according to the documentation, I need to install
$ npm install -g @angular/cli
with the-g
(global) flag.However I would like to have Angular-CLI installed locally with the rest of my node_modules packages. This way, when I've download my project from git, I could simply run
$ npm install
(for installing all the dependencies in my package.json).I try to create a new project by running
$ npm init
and then run$ npm i @angular/cli -D
(-D
is the same as--save-dev
). But then when I run$ ng new project-name
a new sub directory was created with a separate node_modules directory. -
Gil Epshtain over 6 yearsSo according to this answer, once I've created a new project using
ng new project-name
I can deleteangular-cli
from the globalnode_models
and still be able to useng generate X
(sinceangular-cli
is also installed in the localnode_modules
? -
jornare over 6 yearsNo, as this is what is first executed when only typing 'ng' (because it is in the PATH variable), but it will look for another version in your current folders node-modules and execute that if it exist.
-
jornare over 6 yearsTry this: install an old version globally (say 1.5.5) typing ng --version will now yield 1.5.5. Install another version locally (say 1.6.0) typing ng --version will now yield 1.6.0. remove the global version. typing ng --version will not execute.
-
shadowbq over 5 years
npx
is now by default installed with 5.2+ ofnpm
the default manager of node.js. This should be a more widely accepted answer in 2018 -
Robharrisaz over 5 yearsThis was perfect for my issue which was to be able to execute the local package rather than global on a build server where installing global version is not an option.
-
Jennifer S about 4 yearsThe user specifically asked how to do this without using the -g flag.
-
Noumenon about 4 yearsWhen you say "You can go back to using the normal
ng generate
", you mean in your other projects, right? In this project, you will have to usenpx ng generate
. -
Junaid almost 4 yearsNo, just use the same
ng generate
in this project(created using npx) too. No need to disturb npx anymore. In simple words, npx will only be used to create/setup your project with the desired angular version, nothing else. After that just forget about npx. -
McFiddlyWiddly about 3 yearsis
npx -p @angular/[email protected] ng new <project-name>
different fromnpx -p @angular/[email protected] new <project-name>
? -
Carlos Morales about 3 yearsyes they are different. In the first one, you are asking npm to 1) install this package first and then 2) run "ng" command. In the second version you are asking 2) run "new" command, the result will be
new: command not found
-
Volodymyr Gorodytskyi almost 2 years@Junaid thanks for an answer, for experimental purpose I've uninstalled angular cli globally
npm uninstall -g @angular/cli
, than I created new angular project withnpx -p @angular/cli ng new hello-world-project
and after running commandng build
I get errorThe term 'ng' is not recognized as the name of a cmdlet...
, so looks like usingnpx
term is required in this approach.