windows Visual Studio 代码终端,如何以管理员权限运行命令?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37700536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Visual studio code terminal, how to run a command with administrator rights?
提问by Dragod83
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?
新版本 1.2.0 包含一个终端,但是当我尝试使用 node 安装任何包时,我得到了 npm ERR!代码EPERM,我通常解决右键单击并以管理员身份运行它。那么我如何在 vscode 终端中做到这一点呢?有类似 sudo for linux 的东西吗?
回答by CTS_AE
Option 1 - Easier & Persistent
选项 1 - 更简单和持久
Running Visual Studio Code as Administrator should do the trick.
以管理员身份运行 Visual Studio Code 应该可以解决问题。
If you're on Windows you can:
如果您使用的是 Windows,您可以:
- Right click the shortcut or app/exe
- Go to properties
- Compatibility tab
- Check "Run this program as an administrator"
- 右键单击快捷方式或应用程序/exe
- 转到属性
- 兼容性选项卡
- 勾选“以管理员身份运行此程序”
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.
确保您关闭了 VS Code 的所有其他实例,然后尝试以管理员身份运行。电子框架喜欢在关闭进程时停止进程,所以最好检查你的任务管理器并杀死剩余的进程。
Related Changes in Codebase代码库中的相关更改- https://visualstudio.uservoice.com/forums/293070-visual-studio-code/suggestions/8915236-visual-code-w-terminal-integrated-and-super-admin
- https://github.com/Microsoft/vscode/issues/7407
- https://visualstudio.uservoice.com/forums/293070-visual-studio-code/suggestions/8915236-visual-code-w-terminal-integrated-and-super-admin
- https://github.com/Microsoft/vscode/issues/7407
Option 2 - More like Sudo
选项 2 - 更像 Sudo
If for some weird reason this is not running your commands as an Administrator you can try the runas
command. Microsoft: runas command
如果出于某种奇怪的原因,这不是以管理员身份运行您的命令,您可以尝试该runas
命令。微软:runas 命令
runas /user:Administrator myCommand
runas "/user:First Last" "my command"
runas /user:Administrator myCommand
runas "/user:First Last" "my command"
- 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.
- 只是不要忘记在其中包含空格的任何内容周围加上双引号。
- 此外,您很可能从未在管理员帐户上设置密码,因为它会在尝试运行命令时要求您输入密码。如果帐户具有管理员访问权限/权限,您始终可以使用没有管理员用户名的帐户。
回答by Simon_Weaver
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.
事实证明,如果文件被 Windows 锁定,也会发生此错误。
This can occur if :
在以下情况下可能会发生这种情况:
- You are actually running the project
- You have files open in both Visual Studio and VSCode.
- 您实际上正在运行该项目
- 你在 Visual Studio 和 VSCode 中都打开了文件。
Running as admin doesn't get around windows file locking.
以管理员身份运行不会绕过 Windows 文件锁定。
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
我在 VS2017 中新建了一个项目,然后切换到 VSCode 尝试添加更多包。停止项目运行并关闭 VS2017 后,它能够无错误地完成
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.
免责声明:我不确定这是否意味着不需要以管理员身份运行,但如果可能的话尽量避免它,以避免某些流氓包做它不应该做的事情的可能性。
回答by Jacques
Here's what I get.
这是我得到的。
I'm using Visual Studio Code and its Terminal to execute the 'npm' commands.
我正在使用 Visual Studio Code 及其终端来执行“npm”命令。
Visual Studio Code (not as administrator)
PS g:\labs\myproject> npm install bootstrap@3
Visual Studio Code(非管理员)
PS g:\labs\myproject> npm install bootstrap@3
Results in scandir and/or permission errors.
导致 scandir 和/或权限错误。
Visual Studio Code (as Administrator)
Run this command after I've run something like 'ng serve'
Visual Studio Code(以管理员身份)
在我运行类似 'ng serve' 之类的命令后运行此命令
PS g:\labs\myproject> npm install bootstrap@3
PS g:\labs\myproject> npm install bootstrap@3
Results in scandir and/or permission errors.
导致 scandir 和/或权限错误。
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:
Visual Studio Code(以管理员身份 - 关闭和打开 IDE)
如果我已经执行了其他会影响节点模块的命令,我决定首先尝试关闭 Visual Studio Code,以管理员身份打开它,然后运行以下命令:
PS g:\labs\myproject> npm install bootstrap@3
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
我得到的结果是:+ [email protected]
添加了 115 个包并在 24.685 秒内更新了 1 个包
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.
这不是一个永久的解决方案,因为我不想在每次想要执行 npm 命令时继续关闭 VS Code,但它确实解决了问题。