在 NodeJS 中开发时,无法在可视代码中启动调试器



我正在尝试通过可视化代码调试器运行我的 MEAN 应用程序,但我无法启动它。它向我展示了一个Cannot connect to runtime process, timeout after 10000 ms - (reason: Cannot connect to the target: connect ECONNREFUSED 127.0.0.1:58...错误。我正在运行节点 8.1.0 和 npm 5.0.3。这是我的启动配置文件

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Launch via NPM",
            "runtimeExecutable": "npm",
            "windows": {
                "runtimeExecutable": "npm.cmd"
            },
            "runtimeArgs": [
                "run",
                "startdev"
            ],
            "port": 5858,
            "protocol": "inspector"
        }
    ]
}

每当我执行程序时,我都会使用 npm run startdev 运行它,startdev在我的package.json中是一个脚本。如何启动调试器?

编辑:

我正在使用startdev所在的npm run startdev运行该应用程序

"startdev": "concurrently "ng build --watch" "cross-env NODE_ENV=development nodemon ./bin/start.js""

我也尝试了这样的附加配置,但无济于事

{
     "type": "node",
     "request": "attach",
     "name": "Attach to Process",
     "processId": "${command:PickProcess}",
     "port": 5858
}

应使用附加配置将调试器附加到节点进程。

您还需要使用调试标志启动该过程所以像

node --debug <filename>

同样正如 OP 在评论中指出的那样,调试标志已被弃用。所以使用检查标志。

node --inspect <filename>

附加配置如下所示 { "name": "Attach", "type": "node", "request": "attach", "port": 5858, "address": "localhost", "restart": false, "sourceMaps": false, "outFiles": [], "localRoot": "${workspaceRoot}", "remoteRoot": null }

有关它的更多信息,请参见此处的文档