196

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,738
  • 21
  • 105
  • 131
Wauna
  • 2,226
  • 2
  • 10
  • 11

43 Answers43

212

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,672
  • 4
  • 55
  • 75
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 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
  • 52
    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
  • 5
    @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
191

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,738
  • 21
  • 105
  • 131
55

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,738
  • 21
  • 105
  • 131
Arun Surawat
  • 707
  • 6
  • 8
33

In the "Environment Variables"

In the "System variables" section

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

Liam
  • 27,717
  • 28
  • 128
  • 190
A. Morel
  • 9,210
  • 4
  • 56
  • 45
25

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,738
  • 21
  • 105
  • 131
17

Changing the policy to Unrestricted worked for me:

Set-ExecutionPolicy Unrestricted -Scope CurrentUser
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rayan Albouni
  • 169
  • 1
  • 5
12

Installing Angular CLI globally solved my problem.

npm install -g @angular/cli
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rut Shah
  • 1,093
  • 11
  • 13
11

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,738
  • 21
  • 105
  • 131
Sandeep Nandey
  • 394
  • 1
  • 6
  • 19
7

In Powershell, just add npx before ng and it will work:

npx ng serve

npx comes with npm since version 5.2

Jaime Vasquez
  • 121
  • 1
  • 5
  • This is a good work-around if you are working in a place like a large financial institution where your machine is completely locked down and you don't have permissions to modify sys environment variables. – santos May 19 '23 at 15:11
6

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,738
  • 21
  • 105
  • 131
Kirubel
  • 1,461
  • 1
  • 14
  • 33
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,738
  • 21
  • 105
  • 131
R15
  • 13,982
  • 14
  • 97
  • 173
4

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,738
  • 21
  • 105
  • 131
3

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

This can cause problems in running 'ng'.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
shambhu yadav
  • 231
  • 2
  • 13
3

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,738
  • 21
  • 105
  • 131
AleHulk
  • 31
  • 1
3

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,738
  • 21
  • 105
  • 131
3

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,738
  • 21
  • 105
  • 131
vidhya c
  • 131
  • 1
  • 4
  • 2
    you shouldn't install npm packages globally – Tarick Welling Jun 20 '19 at 12:26
  • @TarickWelling please provide a supporting argument for your statement. – Jacob-Jan Mosselman May 26 '20 at 08:13
  • Then why does -g exist? And how are you supposed to get CLI tools without -g? – tpartee Jul 03 '20 at 20:41
  • Command prompt where? An already-opened PowerShell window? On already-opened CMD window? In the start menu somewhere? Inside Visual Studio Code? Something else? – Peter Mortensen Apr 09 '22 at 17:07
  • @Jacob-JanMosselman that was probably a general (good) recommendation because it's the default approach for many junior devs no matter the package scope. For example, there are packages that are used only by the npm scripts defined in package.json (like checking for package licenses during the build), in which case there's no need to install them globally. – dvlsc Oct 10 '22 at 08:29
3
  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,738
  • 21
  • 105
  • 131
Brijesh Ray
  • 779
  • 8
  • 15
3

Fix: Running scripts is disabled on this system

Open PowerShell

Set-ExecutionPolicy RemoteSigned
A

(A: YES TO ALL)

Done!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
HaiNM
  • 27
  • 2
3

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,738
  • 21
  • 105
  • 131
rfcdejong
  • 2,219
  • 1
  • 25
  • 51
3

to fix this error you need to follow these steps.

  1. add the path of %AppData%\npm inside your environment variable click Path and put the path inside.
  2. run your terminal as admin and use this command npm install @angular/cli
  3. now you can run the following command ng version
  4. and that all of you need to do. happy coding!
2

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,738
  • 21
  • 105
  • 131
2

Also you can run the following command to resolve it:

npm install -g @angular/cli
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Brijesh Ray
  • 779
  • 8
  • 15
1

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,738
  • 21
  • 105
  • 131
dvlsc
  • 542
  • 6
  • 14
1

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Deepa
  • 850
  • 1
  • 10
  • 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,738
  • 21
  • 105
  • 131
DavB.cs
  • 579
  • 5
  • 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,738
  • 21
  • 105
  • 131
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,738
  • 21
  • 105
  • 131
1

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,738
  • 21
  • 105
  • 131
Yudgine
  • 19
  • 1
  • 3
1

vs code and native powershell

powershell > run in admin follow same steps

run in vs code admin

npm install -g @angular/cli

close vs code

run vs code not admin

ng will work

Master
  • 11
  • 4
0

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
monofal
  • 1,928
  • 1
  • 13
  • 15
0

Run PowerShell or command prompt not as administrator.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dustydojo
  • 449
  • 5
  • 14
  • 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,738
  • 21
  • 105
  • 131
Steve Scott
  • 1,441
  • 3
  • 20
  • 30
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,738
  • 21
  • 105
  • 131
Sasikumar
  • 828
  • 12
  • 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,738
  • 21
  • 105
  • 131
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,738
  • 21
  • 105
  • 131
NoNaMe
  • 6,020
  • 30
  • 82
  • 110
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,738
  • 21
  • 105
  • 131
SU7
  • 1,586
  • 1
  • 18
  • 26
0

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rut Shah
  • 1,093
  • 11
  • 13
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,738
  • 21
  • 105
  • 131
0
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

This worked for me.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

It can be done without updating environment variables
create new project:

npx @angular/cli@latest new project-name

now you can use ng commands inside that project

0

Delete chocklatey -> u can find your chocklatey folder in C:\ProgramData\chocolatey or check in system envirnment variables

  1. then Re-install node.js
  2. then open cmd in your project
  3. then execute ng add @angular/material
-1

You can use:

npm run build:prod
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user1960740
  • 11
  • 1
  • 2
  • 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
-1

Remove the @ng_module directory and run ng init. this command basically installs all the packages and modules required for the project.

ps: don't remove the package.json and angular.json from the project.