简体   繁体   中英

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?

Thanks in advance!

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

As mentioned by CrazyCoder, it is possible to do this with a shell script. 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:

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.

[WS=WebStorm]

A little variant/addition if you want to debug it in WS as well: (without running WS as root):

Gist: WS terminal window + debug port + remote debug config

  1. I'm using nodemon, so still wanted to use that
  2. Open terminal window (Inside WS: Tools->Open Terminal..)
  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 )
  4. Create a remote debug configuration in WS as explained here and set the port to 40155
  5. Select that debug configuration and hit the WS debug button
  6. Set breakpoints and profit :)

It's then very easy to just go back to the WS terminal window and rerun the server.

To add to krisp's answer: You probably also want passing environment variables from your WS configuration to work.

To do that add -E to sudo in the scipt like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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