153

Today, while working through some basic AngularJS introduction, I ran into a problem.

I opened PowerShell to get going on the project. NPM worked.

I was able to install 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

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Wauna
  • 1,768
  • 2
  • 8
  • 10

37 Answers37

172

The first path in the path variable needs to be the NPM path. Opening the Node.js command prompt I found that the ng command worked there. I dug into the shortcut and found that it references a command to ensure the first Path variable is NPM. To Fix:

  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

Once I did that I was able to close powershell and reopen and all worked.

Anand
  • 9,201
  • 4
  • 54
  • 73
jonrsharpe
  • 107,083
  • 22
  • 201
  • 376
  • After following above steps "ng -v" working but "ng build" not working. It is giving this error "You seem to not be depending on "@angular/core". This is an error." – PSR Oct 02 '19 at 11:49
  • 30
    You can also skip all of this in many cases by just typing "npm run ng...." Npm still usually works in powershell without alteration and 'npm run' will allow you to use ng commands. – SmoveBB Mar 26 '20 at 21:12
  • It worked for me . I just want to understand why we are getting tis error after installing nodejs which is already in path. – Saroj Kumar Sahoo Apr 26 '20 at 05:56
  • Hi , It didn't worked for me ..if anybody can help me on skype or zoom please.....i am trying from couple of days but not able to solve...Please – Laxminarayan Charan Aug 28 '20 at 15:03
  • 3
    @LaxminarayanCharan *"if anybody can help me on skype or zoom"* is not an appropriate request for SO. If you have a specific problem applying the information that's already available on this site, ask a new question with a [mre]. – jonrsharpe Aug 28 '20 at 15:04
  • very first you have to run powershell command Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser second we have to run ng.ps1 using powershell ISE – Anish Sinha Apr 21 '21 at 03:31
  • I did this and now I am pass the not a cmdlet or function error. But now I get, "ng cannot be loaded because running scripts is disabled on this system. – Sam May 28 '21 at 20:35
  • 1
    @Sam that's not specific to the Angular CLI, see e.g. https://stackoverflow.com/q/64633727/3001761 (or the comment above yours, by the look of it). – jonrsharpe May 28 '21 at 20:37
  • @jonrsharpe yes I had to run SetExecutionpolicy RemoteSigned – Sam May 29 '21 at 13:55
  • This saved my life. Last time setting 'Set-ExecutionPolicy RemoteSigned' worked for me. This time it did not. Setting the path worked this time. – Sam Jun 26 '21 at 21:49
149

First set up Node.js, and then run this command to install Angular globally:

npm install -g @angular/cli

Now run the ng command. This worked for me.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
28

I used the following:

npm run ng serve

It worked for me without need to set environment variables.

I had to install the TypeScript package after it:

npm install typescript@">=3.4 <3.6"
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Arun Surawat
  • 309
  • 3
  • 8
26

In the "Environment Variables"

In the "System variables" section

In the "Path" variable and before "C:\Program Files (x86)\nodejs\" add => "%AppData%\npm"

Liam
  • 25,247
  • 27
  • 110
  • 174
A. Morel
  • 7,425
  • 2
  • 46
  • 43
18

This 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 the 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 the cache and repeat the above steps.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
17

Changing the policy to Unrestricted worked for me:

Set-ExecutionPolicy Unrestricted -Scope CurrentUser
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Rayan Albouni
  • 169
  • 1
  • 5
10

Installing Angular CLI globally solved my problem.

npm install -g @angular/cli
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Rut Shah
  • 824
  • 7
  • 11
6

I was getting this error in Visual Studio Code while doing ng-build. Running the below command in cmd fixed my issue:

npm install -g @angular/cli@latest
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
R15
  • 10,727
  • 13
  • 68
  • 139
6

If you do not have access to environment variables (e.g., office machines), you can try to run a command like this:

npm run <your Angular command>

It works as well. You just need to add npm run before ng command.

Example:

npm run ng g c shop/cart

Enter image description here

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Sandeep Nandey
  • 324
  • 1
  • 4
  • 14
4

For the Visual Studio Code Terminal

First open cmd and install angular-cli as global

npm install -g @angular/cli

Then update your environment variables following these steps:

  1. Win + S. This will open a search box
  2. Type Edit Environment Variables
  3. Open Environment Variables
  4. Add %AppData%\npm inside PATH
  5. Click OK and Close.

Now you can restart your Visual Studio Code and it will work as it will normally do.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Kirubel
  • 1,411
  • 8
  • 31
3

If your project name contains '-', remove it and try.

This can cause problems in running 'ng'.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
shambhu yadav
  • 231
  • 2
  • 13
2

Open Edit the system environment variables.

In the "Path" and "PS Module Path" variable, add "%AppData%\npm"

Run Visual Studio Code as Administrator.

It works for me!

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
AleHulk
  • 21
  • 1
2

You just need to close Visual Studio Code and restart again. But to get the ng command to work in Visual Studio 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.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
2
  1. I 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 the error is 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 → PropertiesAdvanced system settingEnvironment Variable → add a path below "User variable" (if 'Path' not there) → C:\Users\brijeshray\AppData\Roaming\npm → save it and restart Visual Studio Code

    Enter image description here

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Brijesh Ray
  • 419
  • 5
  • 9
2

Fix: Running scripts is disabled on this system

Open PowerShell

Set-ExecutionPolicy RemoteSigned
A

(A: YES TO ALL)

Done!

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
HaiNM
  • 17
  • 1
2

All answers are about how to fix it, but the best is to download Node.js and let the installer add to PATH variable.

Version 12 and 13 are too new, so I had to download 11.15.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
rfcdejong
  • 2,031
  • 1
  • 23
  • 49
2

Selected Advanced System Settings

Enter image description here

Clicked "Environment Variables"

Enter image description here

Under "Path" variable, made the first value listed to be %AppData%\npm

Enter image description here

I did that. Close PowerShell and reopen. All worked.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
1

Instead of using the "ng serve" command in the Visual Studio code terminal, open the Angular app path in the command prompt (Run as Administrator).

Then issue "ng serve" command.

Then open a browser and go to the http://localhost:4200/.

It works for me.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
1

You should update Node.js to the latest version. Otherwise uninstall Node.js and install it again.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Deepa
  • 670
  • 9
  • 11
1

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"
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
DavB.cs
  • 399
  • 3
  • 6
1

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

https://cli.angular.io/

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
1

I resolved it by following the below steps:

  1. Right click on command prompt
  2. Run as administrator
  3. Type npm install -g @angular/cli
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
vidhya c
  • 91
  • 1
  • 3
1

Exit everything and run npm i -g @angular/cli in a command prompt

And build your Angular application there itself (do not build on PowerShell).

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
0

I was using npm (5.5.1). Updating it to the latest version solved my problem.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
monofal
  • 1,818
  • 1
  • 12
  • 15
0

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 Visual Studio Code as an administrator.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
dvlsc
  • 494
  • 6
  • 13
0

Run PowerShell or command prompt not as administrator.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
dustydojo
  • 212
  • 4
  • 12
  • This seems to ***contradict*** other answers. What is the explanation? Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/55266327/edit), not here in comments (***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Apr 09 '22 at 16:54
0

After changing the path you have to restart PowerShell. You do not need to restart your computer.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Steve Scott
  • 1,176
  • 3
  • 17
  • 23
0

Based on the previous answers, here is the consolidation.

  1. Run the command npm install -g @angular/cli@project_version

  2. Add the below paths to your environment variables → System VariablesPath (for which administrator 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

  3. Reopen your command prompt from your project folder and run ng serve.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Sasikumar
  • 718
  • 11
  • 25
0

I ran the 'ng serve' command in the command prompt. It compiled the project successfully. Then whatever changes are saved in Visual Studio Code, are automatically refreshed in the browser.

PS: I have installed Angular globally.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
0

Also you can run the following command to resolve it:

npm install -g @angular/cli
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Brijesh Ray
  • 419
  • 5
  • 9
0

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.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
NoNaMe
  • 5,686
  • 30
  • 78
  • 104
0

Even though the correct answers have been given, all of these didn't work for me because:

  • My username didn't have the Administrator privileges and I couldn't update the environment variable like suggested in the answers.

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
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
SU7
  • 1,126
  • 14
  • 23
0

Reinstalling Node.js is also an option. It will set the PATH variable automatically.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Rut Shah
  • 824
  • 7
  • 11
0

In the "Environment Variables"

In the "System variables" section

In the "Path" variable and before "C:\Program Files (x86)\nodejs" add => "%AppData%\npm"

And also do:

npm install

Then restart your Visual Studio Code. This worked for me.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
0

It is helped for me (for Windows):

  1. Delete folders:

    C:\Users\**YourName**\AppData\Roaming\npm
    C:\Users\**YourName**\AppData\Roaming\npm-cache
    
  2. Install Node.js from https://nodejs.org/en/download/

  3. In the terminal:

    npm install -g @angular/cli
    
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Yudgine
  • 9
  • 1
  • 3
0
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

This worked for me.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
-1

You can use:

npm run build:prod
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
user1960740
  • 1
  • 1
  • 1
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 29 '22 at 11:00
  • 1
    This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/31402110) – Booga Roo Mar 31 '22 at 10:47