简体   繁体   中英

How to debug multiple node projects in vscode debugger?

I have two separate node projects in two separate workspaces. I am trying to debug the projects with vscode debugger but I can only debug one project at a time. If I try to launch the debugger for the second project after starting the debugger for the first one, vscode debuggers restarts the first project again.

I have gone through various tutorials and vscode documentation for debugging and vscode debugging for nodejs but to no avail. Following are the launch configurations for both projects.

Project 1(fort):

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Launch fort",
            "runtimeExecutable": "npm",
            "runtimeArgs": [
                "start"
            ],
            "envFile": "${workspaceFolder}/.env",
            "port": 9229
        }
    ]
}

Value for scripts attribute in package.json

"scripts": {
    "start": "node --inspect app.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  }

Project 2(User Management):

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Launch User Management",
            "runtimeExecutable": "npm",
            "runtimeArgs": [
                "start"
            ],
            "envFile": "${workspaceFolder}/.env",
            "port": 9229
        }
    ]
}

Value for scripts attribute in package.json

"scripts": {
    "start": "node --inspect server.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  }

As per my understanding after reading the vscode docs, if I have separate launch.json present in the .vscode folder of the workspaces that particular configuration will be used to launch the debugger.

Perhaps I am missing something in the docs but I have invested enough time and have been unable to figure out a solution.

You need to use two separate ports for the debuggers to attach, eg:

Project 1:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Launch fort",
            "runtimeExecutable": "npm",
            "runtimeArgs": [
                "start"
            ],
            "envFile": "${workspaceFolder}/.env",
            "port": 9228
        }
    ]
}

Or if you want to attach to the process:

{
        "type": "node",
        "request": "attach",
        "name": "Attach",
        "port": 9228
}

Start node inspect on port 9228:

node --inspect=9228 index.js

You can keep the defaults for the second project.

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