33
votes

I'm experiencing an issue where Visual Studio Code in Windows 10 is not able to detect globally installed NPM packages within the Integrated Terminal.

For example, if the windows command prompt I install @angular/cli via command npm install -g @angular/cli, within that command prompt I can execute @angular/cli commands such as ng --version without error. After that global npm module install, within Visual Studio Code, performing the same ng --version gives me the error:

The term 'ng' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

I can however within the integrated terminal successfully perform commands such as npm -v and node -v.

I have the following items in my system environment variables

C:\Program Files\nodejs\

%AppData%\npm

Are there Visual Studio Code user or system environment variables I'm missing to allow detection of global npm modules?

Thank you for any help you can provide.

10
try to change the terminal from powershell to cmd.exe - federico scamuzzi
Changing default shell to cmd.exe gives me the same errors regarding global npm modules. - Alexander Staroselsky
so the problem i think is in the path variable .. check them in the global variables (if there are the path to the npm in AppData Roaming etc etc) - federico scamuzzi
npm install -g @angular/cli fixed it for me (also I updated node using npm) [stackoverflow.com/questions/18412129/… - cluis92

10 Answers

52
votes

Visual Studio Code must be "Run as Administrator" for working Terminal Commands.

26
votes

I was able to resolve the issue and use global npm commands in Visual Studio Code by modifying my system environment path variables to position %AppData%\npm before %ProgramFiles%\nodejs\ per npm troubleshooting tips

18
votes

no need to do anything else just follow below steps -

  1. right click on my computer (this pc).
  2. go to properties.
  3. click on advanced system setting.
  4. click on environmental variable.
  5. select path variable from system variable.
  6. click on edit button.
  7. add %AppData%\npm and %ProgramFiles%\nodejs\ at top of the list. 8 click on ok. ok. ok
  8. restart your system.
  9. after restart open visual studio code as run as administrative mode.
  10. Enjoy...............!!!!!

enter image description here

2
votes

You can resolve the issue by opening the VS Code Terminal and type

npm install -g @angular/cli

This should resolve the issue.

2
votes

Run Visual studio code as an administrator.Then type ng command.It should w

2
votes
  1. Run powershell or cmd prompt in admin mode.
  2. Check if npm and node commands work (if not, then check environment variables as others have mentioned).
  3. Update node using npm [https://stackguides.com/questions/18412129/how-can-i-update- npm-on-windows]
    • Set-ExecutionPolicy Unrestricted -Scope CurrentUser -Force
    • npm install -g npm-windows-upgrade
    • npm-windows-upgrade
  4. Run npm install -g @angular/cli
  5. Run ng --version in Visual Studio Code to see if it's now working.
2
votes

If you have entered the environment variable and it is still not recognized and if you don't want to reboot your computer because you have so many things running then just reboot your Visual Studio Or Visual Studio Code and then Open Terminal (ctrl + `) and run

npm install

Now it will work fine.

1
votes

If you do not have access to Environmental variable (office machines).

You can try to run command like this:

npm run <your angular command>

it works as well, just need to add npm run before ng command

Example -> npm run ng g c shop/cart

enter image description here

0
votes

I found that adding %AppData%\npm to your Path variable made it work for PowerShell and the Command Prompt, it still did not work for the integrated PowerShell in VS Code for me.

When I changed it from %AppData%\npm to the actual path C:\Users\<USER>\AppData\Roaming\npm where <USER> is your user directory, and reopened VS Code it started working.

0
votes

Reboot your computer , this worked for me