Visual studio code terminal, how to run a command with administrator rights?

WindowsNpmVisual Studio-Code

Windows Problem Overview


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

Windows Solutions


Solution 1 - Windows

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.

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.

Solution 2 - Windows

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

Solution 3 - Windows

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

Solution 4 - Windows

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.

Solution 5 - Windows

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.

Solution 6 - Windows

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDragod83View Question on Stackoverflow
Solution 1 - WindowsCTS_AEView Answer on Stackoverflow
Solution 2 - WindowschamView Answer on Stackoverflow
Solution 3 - WindowsDarryl WindsorView Answer on Stackoverflow
Solution 4 - WindowsSimon_WeaverView Answer on Stackoverflow
Solution 5 - WindowsJacquesView Answer on Stackoverflow
Solution 6 - WindowsDilshan MadhurangaView Answer on Stackoverflow