46

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.

Alexander Staroselsky
  • 37,209
  • 15
  • 79
  • 91

12 Answers12

83

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

LittleBobbyTables - Au Revoir
  • 32,008
  • 25
  • 109
  • 114
Jnyanendra Sethi
  • 1,055
  • 1
  • 8
  • 3
36

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

Alexander Staroselsky
  • 37,209
  • 15
  • 79
  • 91
  • Please update the link to: https://docs.npmjs.com/getting-started/troubleshooting – Janos Vinceller Nov 09 '20 at 19:26
  • 1
    For me, it worked only after killing existing terminal instance/ commands in VS code and then adding new terminal. Cause to outcome ratio is staggering though ;) – bharat1 Jan 28 '22 at 14:47
28

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

AKX
  • 152,115
  • 15
  • 115
  • 172
jagdish desai
  • 637
  • 6
  • 4
  • 2
    This solution worked exactly for me. No need to restart your system though. The ng command started working in VS Code Terminal. – Jagdish Chopde Jul 07 '19 at 08:31
  • worked for me. I had some issues updating my NPM packages after windows update and sometimes randomly, after adding these, I don't have any problems so far! – Michael Mar 11 '20 at 07:50
  • for me, %AppData%\npm and %ProgramFiles%\nodejs\ these path do not exists. Am I missing something after vs code installation? – Upendra Gughane Jan 27 '22 at 13:18
6

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

Pathik Vejani
  • 4,263
  • 8
  • 57
  • 98
dev saha
  • 61
  • 1
  • 1
  • 2
    Welcome to SO! When you answer a question try to comment your answer a little bit. In this case, as there are some more answers, you should exposed the Cons and Pros of your answer. If not, just make a comment. – David García Bodego Oct 20 '19 at 06:29
3

Reboot your computer , this worked for me

  • I freshly installed Node.js (NPM) and I had to restart my computer also to get NPM to register in my terminal. – justinl Dec 02 '21 at 01:04
3

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.

Maz341
  • 2,232
  • 15
  • 20
2

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

npm install -g @angular/cli

This should resolve the issue.

Ben
  • 953
  • 12
  • 27
  • 1
    Thanks, it works. Wondering why the terminal for VS code behaves differently from the native one. – Karthick Manoharan Jul 20 '18 at 20:54
  • @KarthickManoharan It is because many of the environment variables you need to define for windows is predefined there. BTW did you downvote the answer? – Ben Jul 21 '18 at 13:42
2
  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://stackoverflow.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.
cluis92
  • 664
  • 12
  • 35
2

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

Sandeep Nandey
  • 394
  • 1
  • 6
  • 19
0

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.

Brad Lawrence
  • 1,483
  • 1
  • 11
  • 11
0

To me the solution was adding the extensions for npm: enter image description here

Laura Liparulo
  • 2,849
  • 26
  • 27
0

Additionaly to all the good advice here, make sure that you have node.js installed on your machine.

myroslav
  • 1,670
  • 1
  • 19
  • 40