vscode:使用 nodemon 启动时无法使用 Visual Studio 代码在任何断点处中断

vscode: Can't break on any breakpoint with Visual Studio code when launching with nodemon

VSCode版本:1.10.2 OS 版本:Windows 7 专业版,SP1 节点版本:6.10.0

大家好。

我正在尝试使用 visual studio 代码在服务器端调试 typescript 代码(或 javascript 代码),当使用 nodemon 启动它时。我在 launch.json 中添加了一个新配置,如下所示:

{
      "type": "node",
      "request": "launch",
      "name": "Launch server with Nodemon",
      "runtimeExecutable": "nodemon",
      "runtimeArgs": [
        "--debug=5858"
      ],
      "program": "${workspaceRoot}/src/server.ts",
      "restart": true,
      "port": 5858,
      "console": "integratedTerminal",
      "internalConsoleOptions": "neverOpen",
      "outFiles": ["${workspaceRoot}/build/**/*.js"]
    }

我在 vscode 中有一项任务是 运行 tsc,它可以正确构建 javascript 文件。这是我当前的任务配置:

{
  // See https://go.microsoft.com/fwlink/?LinkId=733558
  // for the documentation about the tasks.json format
  "version": "0.1.0",
  "command": "tsc",
  "isShellCommand": true,
  "args": ["-p", "."],
  "showOutput": "silent",
  "problemMatcher": "$tsc"
}

当我更改 typescript 文件时,javascript 文件按预期生成。 当生成 javascript 文件时,nodejs 服务器正在按预期重新启动。

但我无法在任何断点处中断(在打字稿文件或 javascript 文件上)。

你能告诉我这是一个问题还是我遗漏了什么?

感谢您的帮助

vscode 中似乎存在问题(问题由 github [此处][1] 上打开)。但目前,解决方法是将配置 (launch.json) 中的协议设置为 "inspector"。使用此选项,现在可以正确到达断点。

另外,将"runtimeArgs"选项中的"--debug=5858"改为"--inspect=5858"[=36] =]

{
  "type": "node",
  "request": "launch",
  "name": "Launch server with Nodemon",
  "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/nodemon",
  "runtimeArgs": [
    "--inspect=5858"
  ],
  "program": "${workspaceRoot}/src/server.ts",
  "restart": true,
  "port": 5858,
  "protocol": "inspector",
  "console": "integratedTerminal",
  "internalConsoleOptions": "neverOpen",
  "outFiles": ["${workspaceRoot}/build/**/*.js"],
  "sourceMaps": true
},

此外,在那之后,如果您有一条闪烁的消息错误告诉您:

Cannot connect to runtime process, timeout after 10000 ms - (reason: Cannot connect to the target: connect ECONNREFUSED 127.0.0.1:5858)

这意味着您的程序太短,调试器没有足够的时间在您的断点处中断。要解决这个问题,请向选项 "runtimeArgs" 添加第二个 运行time 参数:"--debug-brk" 并设置 "stopOnEntry"选项为true

最终配置应如下所示:

{
  "type": "node",
  "request": "launch",
  "name": "Launch server with Nodemon",
  "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/nodemon",
  "runtimeArgs": [
    "--inspect=5858",
    "--debug-brk"
  ],
  "stopOnEntry": true,
  "program": "${workspaceRoot}/src/server.ts",
  "restart": true,
  "port": 5858,
  "protocol": "inspector",
  "console": "integratedTerminal",
  "internalConsoleOptions": "neverOpen",
  "outFiles": ["${workspaceRoot}/build/**/*.js"],
  "sourceMaps": true
}

它应该在您输入的 javascript 文件的第一行中断。然后你可以按F5,它会到达你自己的断点。

如果您不想每次 运行 您的程序都按 F5,您可以将主入口代码嵌入至少 1000 毫秒超时的 setTimeOut 函数中。

所有这些选项都会给 vscode 足够的时间来打破你的断点。

://github.com/Microsoft/vscode/issues/23900 "GitHub Issue"

@Philoufelin,感谢您的努力。我现在已经按照您的建议对它们进行了测试。

... 添加到文件 tsconfig.json

"outDir": "./dist"

来自文件 launch.json

的快照
"configurations": [
   {  
     "type": "node",
     "request": "launch",
     "name": "nodemon",
     "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/nodemon",
     "runtimeArgs": [ "--inspect=5858" ],
     "program": "${workspaceRoot}/src/server/main.ts",
     "restart": true,
     "port": 5858,
     "protocol": "inspector",
     "console": "integratedTerminal",
     "internalConsoleOptions": "neverOpen",
     "outFiles": ["${workspaceRoot}/dist/**/*.js"],
     "sourceMaps": true
   }
 ]

但是不行。 10 秒后 vscode 闪烁消息 ...
无法连接到运行时进程,10000 毫秒后超时 -(原因:无法连接到目标:连接 ECONNREFUSED 127.0.0.1:5858)

nodemon 还在启动时打印出以下行:
chrome-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:5858/a0f8f52a-47ec-4ddb-9f03-4eb1c97ef8aa

我在 Chromium 中试过 link,附件有效,但调试器语句或断点被忽略。

我观察到 vscode 与正常调试的另一个区别。正常调试从 DEBUG CONSOLE 选项卡开始。在 TERMINAL 选项卡中启动 nodemon。

@ManfredSteiner

我最近也有这个问题。我猜你已经尝试在你的条目文件的开头中断 (main.ts)。我听说我们收到此错误消息是因为程序太短并在调试器成功附加之前终止。您有 2 个解决方案:

  • 首先,将您的入口代码放在至少 1000 毫秒的 setTimeOut 函数中。它应该给调试器足够的时间来附加到您的程序。

  • 第二个解决方案是将 launch.json 选项设置为:"stopOnEntry" 到 "true" 并将 运行timeArgs 选项设置为 [" --inspect=5858", "--debug-brk"].

像这样: "configurations": [ { "type": "node", "request": "launch", "name": "nodemon", "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/nodemon", "runtimeArgs": [ "--inspect=5858", "--debug-brk" ], "stopOnEntry": true, "program": "${workspaceRoot}/src/server/main.ts", "restart": true, "port": 5858, "protocol": "inspector", "console": "integratedTerminal", "internalConsoleOptions": "neverOpen", "outFiles": ["${workspaceRoot}/dist/**/*.js"], "sourceMaps": true } ]

它将在您的 main.js 的第一行中断,然后按 F5,您将能够在 main.ts 中您自己的断点处中断。如果你不想,每次你 运行 你的程序,按 F5 直到它到达你的断点,我建议你使用第一个解决方案(将你的 main.ts 入口代码嵌入到 setTimeOut 函数中至少 1000 毫秒)。希望对你有所帮助