191

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

3
  • 11
    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
    Commented Jun 9, 2016 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? Commented Jan 1, 2018 at 19:24
  • 3
    ` just run visual studio code as administrator. Just right click on it.' it works only if you close all instances of VScode before
    – JinSnow
    Commented May 31, 2019 at 11:05

13 Answers 13

240

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.
9
  • 1
    The default location for the executable is C:\Program Files\Microsoft VS Code\Code.exe
    – bbsimonbb
    Commented Apr 3, 2018 at 8: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" Commented Apr 25, 2018 at 14:16
  • 2
    You will not be able to use mapped drives in your project or code with Option 1. stackoverflow.com/a/32715171/5060792
    – Clay
    Commented Sep 22, 2019 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. Commented Mar 22, 2020 at 9: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}. Commented Mar 22, 2020 at 9:35
81

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

7
  • 5
    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? Commented Dec 29, 2020 at 23:28
  • 1
    This even has the benefit that you don't have to confirm running VSC as an administrator on startup.
    – lema
    Commented Jan 11, 2021 at 13:36
  • This is the correct answer! Kudos to you, sir!
    – Paulo
    Commented Apr 30, 2021 at 23:26
  • 2
    Works, sadly it isn't persistent, unless I'm missing something. Will stick to Option 1 of the accepted answer.
    – robro
    Commented Nov 2, 2021 at 12:58
  • 1
    Make sure all Visual Studio Code windows are closed. If any non-administrator windows are open, the new window won't have administrator privileges. Commented Nov 15, 2021 at 21:50
7

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

1
  • Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope LocalMachine to affect all users and all sessions. see also Get-ExecutionPolicy -List to see more Commented Dec 8, 2022 at 17:20
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.

1
  • 1
    Thanks, this is exactly what happened to me. Closing the other applications that uses the folder and then reinstalling via cmd in Admin. Commented Jun 5, 2020 at 3:35
4

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

2
  • tried the best way but now my terminal isnt loading properly :/ niether bash nor powershell load. I cant type anything into them. Commented Jul 19, 2022 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 Commented Jul 19, 2022 at 21:29
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: + [email protected]
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.

3
  • I also have the same issue with my new laptop. @Jacques, do you know why this happen?
    – Ivan
    Commented Oct 22, 2021 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
    Commented Oct 25, 2021 at 6: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
    Commented May 1, 2022 at 7:20
3

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.
2

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"

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.

1

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.

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.

0

Open power-shell as administrator and run

Set-ExecutionPolicy -ExecutionPolicy Unrestricted
0

None of the above worked for me on windows 11, as of 2024jan. This is the most consistent behavious,which does not require you to do the

  1. Right click in the folder, open in terminal (you can also open terminal as an adminsitrator if you like). Then run python myscript.py and it will work.

Enabling administrator mode in the compatibility tab of the app works in few scenarios but the one i suggested should work in all scenarios.

Not the answer you're looking for? Browse other questions tagged or ask your own question.