简体   繁体   English

WebStorm如何以root身份运行nodejs来执行shell命令?

[英]WebStorm How run nodejs as root to execute shell commands?

How Can I run a node.js application through WebStorm IDE with root privileges in order to let node.js run shell commands? 如何通过具有root权限的WebStorm IDE运行node.js应用程序,以便让node.js运行shell命令?

Thanks in advance! 提前致谢!

最简单的方法是使用root运行WebStorm本身,父节点进程将继承权限。

As mentioned by CrazyCoder, it is possible to do this with a shell script. 正如CrazyCoder所提到的,可以使用shell脚本执行此操作。 To elaborate, the script would look something like this: 详细说明,脚本看起来像这样:

#!/bin/bash
sudo /path/to/node "$@"

Additionally one would want to run sudo visudo and add the following line: 另外,人们想要运行sudo visudo并添加以下行:

username ALL=(ALL) NOPASSWD: /path/to/node

Finally, one would want to chmod +x the script and then specify the path to the script as the path to node in WebStorm. 最后,人们希望chmod +x脚本,然后指定脚本的路径作为WebStorm中节点的路径。

[WS=WebStorm] [WS = WebStorm]

A little variant/addition if you want to debug it in WS as well: (without running WS as root): 如果你想在WS中调试它,还有一点变体/添加:(不以root身份运行WS):

Gist: WS terminal window + debug port + remote debug config 要点:WS终端窗口+调试端口+远程调试配置

  1. I'm using nodemon, so still wanted to use that 我正在使用nodemon,所以仍然想使用它
  2. Open terminal window (Inside WS: Tools->Open Terminal..) 打开终端窗口(内部WS:工具 - >打开终端..)
  3. In the terminal run sudo nodemon --debug=40155 --nolazy server.js (40155 is the debug port to connect to later, you could just run node instead of nodemon ) 在终端运行sudo nodemon --debug=40155 --nolazy server.js (40155是稍后连接的调试端口,你可以只运行node而不是nodemon
  4. Create a remote debug configuration in WS as explained here and set the port to 40155 在WS创建远程调试配置作为解释这里和端口设置为40155
  5. Select that debug configuration and hit the WS debug button 选择该调试配置并单击WS调试按钮
  6. Set breakpoints and profit :) 设置断点和利润:)

It's then very easy to just go back to the WS terminal window and rerun the server. 然后返回WS终端窗口并重新运行服务器非常容易。

To add to krisp's answer: You probably also want passing environment variables from your WS configuration to work. 添加到krisp的答案:您可能还希望从WS配置中传递环境变量。

To do that add -E to sudo in the scipt like this 为此,在这个scipt中添加-Esudo

#!/bin/bash
sudo -E node "$@"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM