简体   繁体   English

如何在vscode调试器中调试多个节点项目?

[英]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. 我正在尝试使用vscode调试器调试项目,但一次只能调试一个项目。 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. 如果在启动第一个项目的调试器后尝试启动第二个项目的调试器,则vscode调试器将重新启动第一个项目。

I have gone through various tutorials and vscode documentation for debugging and vscode debugging for nodejs but to no avail. 我已经遍历了各种教程和vscode文档,以进行调试和nodejs的vscode调试,但都无济于事。 Following are the launch configurations for both projects. 以下是两个项目的启动配置。

Project 1(fort): 项目1(努力):

{
    "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 package.json scripts属性的值

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

Project 2(User Management): 项目2(用户管理):

{
    "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 package.json scripts属性的值

"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. 根据我对vscode文档的理解,如果我在工作区的.vscode文件夹中存在单独的launch.json ,则将使用特定的配置来启动调试器。

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: 项目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: 在端口9228上启动节点检查:

node --inspect=9228 index.js

You can keep the defaults for the second project. 您可以保留第二个项目的默认值。

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

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