Today, while working through some basic AngularJS Intro, I ran into a problem.
I opened PowerShell to get going on the project. NPM worked.
I was able to install the Angular using:
npm install -g @angular/cli
Anytime I tried to run ng, I would get:
the term 'ng' is not recognized as the name of a cmdlet
This question is related to
node.js
angular
powershell
npm
angular-cli
I used the following:
npm run ng serve
It worked for me without need to set Environment variables.
I had to install a typescript after it:
npm install typescript@">=3.4 <3.6"
Open Edit the system environment variables
In the "Path" and "PS Module Path" variable add "%AppData%\npm"
Run Visual Code as Administrator
It works for me!
Even though the correct answers have been given. But all of these didn't work for me because:
So for all of you who face the same issue as me here is what I did:
Instead of ng serve
I copy-pasted the complete location path of ng
like the following and it worked.
So the ng serve
command became:
C:\Users\MyUserName\AppData\Roaming\npm\ng.cmd serve
For VSCode Terminal
First open cmd
and install angular-cli
as global
npm install -g @angular/cli
Then update your Environment Variables following this step
win + s
// this will open search box%AppData%\npm
inside PATH
OK
and close.Now you can restart your VSCode
and it will work as it will normally do.
All answers are about how to fix it, but the best is to download nodeJs and let the installer add to PATH variable.
Version 12 and 13 are too new, so I had to download 11.15 https://nodejs.org/download/release/v11.15.0/
You should update node js to latest version. Otherwise uninstall node js and install it again.
You can also make sure you run the Command Prompt - or whatever terminal you use - As Administrator. I am using Visual Studio Code and the ng serve
command gives me that exact error when not running VS Code as admin.
Instead of giving "ng serve" command in the Visual Studio code terminal, open angular app path in the command prompt(Run as Administrator).
Then give "ng serve" command.
Then open browser and go to the http://localhost:4200/
It works for me.
Reinstalling NodeJs is also an option. It will set PATH variable automatically.
Solution Worked For me :
Add a path to your environment Variable
C:\Users\YourPcName\AppData\Roaming\npm
as well as your bin folder of the angular file [present their itself]
C:\Users\YoutPcName\AppData\Roaming\npm\node_modules\angular-cli\bin
and then run
ng -v
it will pop up angular cli gui in your Command prompt.
Note After running npm i -g @angular/cli
do restart your command prompt and check if it works otherwise clean cache and repeats the above steps.
I was using npm (5.5.1) updating it to latest version solved my problem.
After changing the path you have to restart powershell. You do not need to restart your computer.
First setup Node.js, then go to your project folder with the command prompt such as D:\project and then run this command:
npm install -g @angular/cli
Now run ng command. This work for me.
exit everything and run npm i -g @angular/cli in command prompt , and build your angular app there itself(do not build on powershell)
You just need to close the visual studio code and restart again. But to get the ng command work in vs code, you need to first compile the project with cmd in administrator mode.
I was also facing the same problem. But this method resolved it.
Please also make sure that the node_modules folder also exists there in the project directory. If it is not there you will get a similar issue. so please run npm install as well.
Based on the above answers here is the consolidation.
npm install -g @angular/cli@project_version
Add the below paths to your environment variables -> System Variables -> Path (For which Admin access is required).
C:\Users\YourPcAccountName\AppData\Roaming\npm
C:\Users\YoutPcAccountName\AppData\Roaming\npm\node_modules\angular-cli\bin
Make sure the first value listed as %AppData%\npm
ng serve
.If your project name contain '-'. Remove it and try. This can cause problem in running 'ng'.
Selected Advanced System Settings enter image description here Clicked "Environment Variables" enter image description here Under "Path" variable, made the FIRST value listed %AppData%\npm enter image description here I did that.Close powershell and reopen. all worked
I ran the 'ng serve' command in the command prompt. It compiled the project successfully. Then whatever changes are saved in VS Code, are automatically refreshed in the browser.
PS: I have installed angular globally.
I resolved by following the below steps
1.Right click on command Prompt 2.Run as administrator 3.type npm install -g @angular/cli
This powershell script worked to add the correct setting to my Environment Variable "Path" ( As a per-user setting. ) It adds: %AppData%\npm ...and then restart the command line that uses "ng"
$existingPath = [System.Environment]::GetEnvironmentVariable("Path","User")
write-host "existing PATH variable is $existingPath"
$newPath = "%AppData%\npm;$existingPath"
write-host "new PATH will be $newPath"
# update here
[System.Environment]::SetEnvironmentVariable("Path", $newPath, "User")
$finalPath = [System.Environment]::GetEnvironmentVariable("Path","User")
write-host "final PATH variable is $finalPath"
1)Right Clicked on My Computer (windows) 2)Selected Advanced System Settings 3)Clicked "Environment Variables" 4)Under "Path" variable, made the FIRST value listed %AppData%\npm
Initially: C:\Program Files\Microsoft MPI\Bin\;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files\TortoiseSVN\bin;
After path adding: C:\Program Files\Microsoft MPI\Bin\;%AppData%\npm;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files\TortoiseSVN\bin;
One more thing you can try ,if error still coming, as below 1)go to project location via command prompt:C:\Users\brijeshray\ParentChild> 2)Reinstall or update existing angular as : npm install -g@angular/cli@latest
3)Go to computer or PC-->Properties-->Advanced system setting -->Environment Variable -->add a path below "User variable"(if 'Path' not there) ==>C:\Users\brijeshray\AppData\Roaming\npm -->save it and restart visual code
In the "Environment Variables"
In the "System variables" section
In the "Path" variable and before "C:\Program Files (x86)\nodejs\"
add => "%AppData%\npm"
The problem is NOT the install of the NPM nor the path ! If you want to use the "ng" command you need to install the angular-cli. by running the following command
npm install -g @angular/cli
Also you can run following command to resolve, npm install -g @angular/cli
Run Powershell or command prompt not as administrator.
Installing angular cli globally solved my problem.
npm install -g @angular/cli
Fix: Running scripts is disabled on this system
Open Powershell
Set-ExecutionPolicy RemoteSigned A
(A: YES TO ALL)
Done!
I was getting this error in Visual Studio Code
while doing ng-build
. Running below command in cmd fixed my issue
npm install -g @angular/cli@latest
Changing the policy to Unrestricted worked for me:
Set-ExecutionPolicy Unrestricted -Scope CurrentUser
Source: Stackoverflow.com