Kaiser.Wang
Kaiser.Wang

Reputation: 71

How can I debug node in VSCode with sudo in mac?

I want to debug node like this in terminal:

$sudo node app

But, in vscode, I don't know where I can configure it.

This is my launch.json. Nothing has helped,

launch.json

Upvotes: 7

Views: 7472

Answers (3)

Paul Han
Paul Han

Reputation: 409

First, create attach process config:

    {
        "name": "Attach to Process",
        "type": "node",
        "request": "attach",
        "port": 9222
    }

and run it.

Second, run node to debug like this.

sudo node --inspect=9222 app.js

Upvotes: 3

Kenn Roberson
Kenn Roberson

Reputation: 181

I have the same issue. I'm trying to debug an app that connects to port 443 on a Mac. In order to get around the problem I've been using I added the following to my package.json under scripts.

"debug": "sudo devtool server.js",

With this you can debug, just not as nice since you are using the chrome debugger verses the one in vscode. You will need to have devtool from chrome installed for this to work.

Upvotes: 0

Erik Elkins
Erik Elkins

Reputation: 629

In order to run the command as sudo you have to launch VS code under sudo. Here's how you do that ...

  1. Install "code" as a shell command if you haven't already by opening visual studio code regularly then opening the command palette (press F1) and type "Shell Command". This will bring up "Install 'code' command in PATH". More on that here: https://code.visualstudio.com/Docs/editor/setup#_mac-os-x
  2. Run "sudo code" in your terminal, and you should see that when you run "node app" it'll be running under sudo.

Upvotes: 6

Related Questions