137

The new version 1.2.0 include a terminal, but when I try to install any pack with node I get the npm ERR! code EPERM that I usually solve right clicking and running it as administrator. So how I do that in the vscode terminal? There is something like sudo for linux?

vscode terminal

CTS_AE
  • 10,343
  • 7
  • 50
  • 55
Dragod83
  • 1,707
  • 3
  • 15
  • 19
  • 7
    Found the answer myself, just run visual studio code as administrator. Just right click on it. Don't know why I didn't think of that earlier. – Dragod83 Jun 09 '16 at 14:29
  • is there no other workaround to this? I've been running Visual Studio for years as admin but this just seems 'wrong' somehow. Also sometimes in VSCode I don't have to do this. Is it only when a package is being deleted or updated that you'd get this? – Simon_Weaver Jan 01 '18 at 19:24
  • ` just run visual studio code as administrator. Just right click on it.' **it works only if you close all instances of VScode before** – JinSnow May 31 '19 at 11:05

6 Answers6

187

Option 1 - Easier & Persistent

Running Visual Studio Code as Administrator should do the trick.

If you're on Windows you can:

  1. Right click the shortcut or app/exe
  2. Go to properties
  3. Compatibility tab
  4. Check "Run this program as an administrator"
There is a caveat to it though

Make sure you have all other instances of VS Code closed and then try to run as Administrator. The electron framework likes to stall processes when closing them so it's best to check your task manager and kill the remaining processes.

Related Changes in Codebase

Option 2 - More like Sudo

If for some weird reason this is not running your commands as an Administrator you can try the runas command. Microsoft: runas command

Examples
  • runas /user:Administrator myCommand
  • runas "/user:First Last" "my command"
Notes
  • Just don't forget to put double quotes around anything that has a space in it.
  • Also it's quite possible that you have never set the password on the Administrator account, as it will ask you for the password when trying to run the command. You can always use an account without the username of Administrator if it has administrator access rights/permissions.
CTS_AE
  • 10,343
  • 7
  • 50
  • 55
  • 1
    The default location for the executable is `C:\Program Files\Microsoft VS Code\Code.exe` – bbsimonbb Apr 03 '18 at 08:06
  • 1
    when I use runas "user:domain\adname" "npm install -g @angular/cli" I get an error "RUNAS ERROR: Unable to run - npm install -g @angular/cli the system cannot find the file specified" – Tom McDonald Apr 25 '18 at 14:16
  • If it's unable to find npm you may need to add your node install to the System `PATH` environment variable. You can also try to verify if it exists by doing something like `where.exe npm` or `npm -v` to display the version. Also if you're using powershell sometimes you have to add exe at the end so you would have something like: `runas.exe /user:Administrator myCommand` – CTS_AE Feb 26 '19 at 23:43
  • 1
    You will not be able to use mapped drives in your project or code with Option 1. https://stackoverflow.com/a/32715171/5060792 – Clay Sep 22 '19 at 13:26
  • 2
    Like the answer mentions, you might find that you've never set your administrator password, if that's the case, leaving the password blank won't work (on Windows 10 at least), but you might also find that your own user account is also an administrator, so use your own user name instead of `Administrator` and you'll achieve the same result. – Tomáš Hübelbauer Mar 22 '20 at 09:23
  • 1
    Also, the integrated terminal won't be reused if you use `runas`, instead, a new console window will open where the argument command will be executed. It will close itself afterwards, to keep it open, use `cmd /k {command}`. – Tomáš Hübelbauer Mar 22 '20 at 09:35
  • Yes. Running as administrator worked for me. Thanks :) – Anurag May 26 '20 at 07:50
  • 1
    _Make sure you have all other instances of VS Code closed_ Ugh. 20 minutes wasted before landing here. Thank You. – Johnny Mopp Mar 10 '22 at 21:27
  • 1
    @JohnnyMopp It drives me crazy that electron apps still don't close properly on Windows like: Spotify, Slack, Discord, Todoist, VSCode; I think one of them does handle it properly, but it drives me nuts that they all never fully close : \ – CTS_AE Mar 11 '22 at 21:00
48

Step 1: Restart VS Code as an adminstrator

(click the windows key, search for "Visual Studio Code", right click, and you'll see the administrator option)

Step 2: In your VS code powershell terminal run Set-ExecutionPolicy Unrestricted

cham
  • 6,265
  • 5
  • 44
  • 61
  • 4
    This works. I don't know why everyone is still upvoting other answers when this is working. Have I missed something, is there a drawback? – Jamie Marshall Dec 29 '20 at 23:28
  • This even has the benefit that you don't have to confirm running VSC as an administrator on startup. – lema Jan 11 '21 at 13:36
  • This is the correct answer! Kudos to you, sir! – ito Apr 30 '21 at 23:26
  • This worked for me. Option 1 in the the accepted is not working – user3731783 Jul 08 '21 at 17:16
  • 1
    Works, sadly it isn't persistent, unless I'm missing something. Will stick to Option 1 of the accepted answer. – robro Nov 02 '21 at 12:58
  • Make sure all Visual Studio Code windows are closed. If any non-administrator windows are open, the new window won't have administrator privileges. – Aaron Jensen Nov 15 '21 at 21:50
4

In my case even while running as admin I had to set the execution policy.

In the terminal type:

Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope Process

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7.2

3

Running as admin didn't help me. (also got errors with syscall: rename)

Turns out this error can also occur if files are locked by Windows.

This can occur if :

  • You are actually running the project
  • You have files open in both Visual Studio and VSCode.

Running as admin doesn't get around windows file locking.

I created a new project in VS2017 and then switched to VSCode to try to add more packages. After stopping the project from running and closing VS2017 it was able to complete without error

Disclaimer: I'm not exactly sure if this means running as admin isn't necessary, but try to avoid it if possible to avoid the possibility of some rogue package doing stuff it isn't meant to.

Simon_Weaver
  • 130,769
  • 75
  • 612
  • 659
  • Thanks, this is exactly what happened to me. Closing the other applications that uses the folder and then reinstalling via cmd in Admin. – Annie Lagang Jun 05 '20 at 03:35
3

Here's what I get.

I'm using Visual Studio Code and its Terminal to execute the 'npm' commands.

Visual Studio Code (not as administrator)
PS g:\labs\myproject> npm install bootstrap@3

Results in scandir and/or permission errors.

Visual Studio Code (as Administrator)
Run this command after I've run something like 'ng serve'

PS g:\labs\myproject> npm install bootstrap@3

Results in scandir and/or permission errors.

Visual Studio Code (as Administrator - closing and opening the IDE)
If I have already executed other commands that would impact node modules I decided to try closing Visual Studio Code first, opening it up as Administrator then running the command:

PS g:\labs\myproject> npm install bootstrap@3

Result I get then is: + bootstrap@3.3.7
added 115 packages and updated 1 package in 24.685s

This is not a permanent solution since I don't want to continue closing down VS Code every time I want to execute an npm command, but it did resolve the issue to a point.

Jacques
  • 6,536
  • 8
  • 41
  • 98
  • I also have the same issue with my new laptop. @Jacques, do you know why this happen? – Ivan Oct 22 '21 at 21:42
  • @Ivan absolutely no idea. Although I haven't had this happen in a while, but I can't tell you what changed – Jacques Oct 25 '21 at 06:50
  • 1
    According to the documentation -the integrated terminal shell is running with the permissions of VS Code. This may be true, but in my case (problems with NVM commands), and in using Bash as my default terminal, I had to *also* set the permissions for GitBash itself to always run as administrator. Easy to accomplish in Bash with a right click on exe location > properties > compatibility > run as administrator, not sure of a persistent way to do it in Powershell (or Windows Terminal). – chrisz May 01 '22 at 07:20
3

There are two ways you can solve this problem,

For temporary
You can follow these steps for it,

  1. Search Vscode after in the windows.
  2. Then right-click and run the VScode as an administrator.

But you can do this every time when you open the VScode.So I supposed it is not the best way to solve this problem.

Best way
You can follow these steps for it,

  1. Search Vscode after in the windows.
  2. Then right-click and click the open file location.
  3. Then you can click the VScode icon and go to the properties.
  4. Next you want to go to the compatibility tab.
  5. At the bottom of the tab, there are selection boxes, in there you can see run this program as an administrator selection box you want to tick it.
  6. Then you can apply those changes and save it.

You can refer to below page to do more things related to this run as administrator stuffs
https://www.sevenforums.com/tutorials/11841-run-administrator.html