179

The new version 1.2.0 includes a terminal, but when I try to install any pack with Node.js, I get the "npm ERR! code EPERM" that I usually solve by right-clicking and running it as administrator. How can I do that in the Visual Studio Code terminal? Is there something like sudo for Linux?

Visual Studio Code terminal

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dragod83
  • 2,127
  • 3
  • 17
  • 20
  • 10
    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
  • 2
    ` 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

11 Answers11

231

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 application (EXE file)
  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 Visual Studio 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 the 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.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
CTS_AE
  • 12,987
  • 8
  • 62
  • 63
  • 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
  • 2
    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. – 001 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
75

Step 1: Restart Visual Studio Code as an administrator

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

Step 2: In your Visual Studio Code PowerShell terminal run Set-ExecutionPolicy Unrestricted

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
cham
  • 8,666
  • 9
  • 48
  • 69
6

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

It 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 Visual Studio Code.

Running as administrator doesn't get around Windows file locking.

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

Disclaimer: I'm not exactly sure if this means running as administrator 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689
  • 1
    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
5

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://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7.2

  • `Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope LocalMachine` to affect all users and all sessions. see also `Get-ExecutionPolicy -List` to see more – metablaster Dec 08 '22 at 17:20
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,936
  • 8
  • 43
  • 102
  • 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

  • tried the best way but now my terminal isnt loading properly :/ niether bash nor powershell load. I cant type anything into them. – VibrationPositive Jul 19 '22 at 21:27
  • turned off run as admin in compatibility mode via right clicking the shortcut and accessing it via properties now it loads again but not sure about if I have admin rights – VibrationPositive Jul 19 '22 at 21:29
1

The only thing that solved the issue for me:

Right-click on the Visual Studio Code EXE file → Compatibility → Check "Run this program as administrator"

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Aleks
  • 894
  • 10
  • 14
1

Creating a shortcut and setting Run As Administrator on the Compatibility tab doesn't just set the Visual Studio Code to run as administrator when opened from that shortcut. It sets the underlying executable to run as administrator no matter how it is opened, which means you lose the ability to run Visual Studio Code in normal non-elevated mode.

As a solution that is a non-starter for me.

Rather than enable administrator mode in the Compatibility tab I find it easier to just open the normal Visual Studio Code shortcut using Ctrl + Shift + Enter instead of enter, that also runs it elevated, but I can still run Visual Studio Code normally by just opening it using Enter.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Neutrino
  • 8,496
  • 4
  • 57
  • 83
1

From MS: "With the introduction of process sandboxing (discussed in this blog post) running as administrator is currently unsupported when AppLocker is configured due to a limitation of the runtime sandbox"

The work around is to disable sandboxing but starting VS Code with the following arguments:

--no-sandbox --disable-gpu-sandbox 
  1. Create a shortcut.
  2. Edit shortcut properties
  3. Set the Target field to "C:\Program Files\Microsoft VS Code\Code.exe" --no-sandbox --disable-gpu-sandbox
  4. Right click on shortcut and Run as Admin.
Squibly
  • 335
  • 2
  • 7
0

Win + R, or from another cmd, type:

    powershell -Command "Start-Process code -Verb RunAs"
    powershell "start code -v runAs"

The second line is just a shorter version. An empty cmd will pop up, but you can close it.

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

There is a much easier way to run commands with Administrator rights from the command line to get the Linux sudo experience in Windows, with none of the issues mentioned in the other answers. Output of commands run with sudo appears below the command in the same Terminal window as the command as expected, no need to quote usernames, set passwords, etc.: sudo for Windows.

You can find the sudo script in the psutils script collection in the Github repository at: https://github.com/lukesampson/psutils

You can install sudo easily using Scoop. At a command prompt, run:

scoop install sudo

To run a command with Administrator rights, simply run it with sudo:

sudo .\myscript.ps1

You will be shown a UAC dialog before the command is run.

ack
  • 7,356
  • 2
  • 25
  • 20