【问题标题】:vscode: Can't break on any breakpoint with Visual Studio code when launching with nodemonvscode:使用 nodemon 启动时,无法使用 Visual Studio 代码中断任何断点
【发布时间】:2017-08-30 06:09:17
【问题描述】:

VSCode 版本:1.10.2 操作系统版本:Windows 7 专业版,SP1 节点版本:6.10.0

大家好。

当使用 nodemon 启动它时,我正在尝试使用 Visual Studio 代码在服务器端调试打字稿代码(或 javascript 代码)。我在 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"]
    }

我在运行 tsc 的 vscode 中有一个任务,它可以正确构建 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"
}

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

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

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

感谢您的帮助

【问题讨论】:

  • 为了调试,我通常只在我的控制台上写这个: node --inspect --debug-brk my-file.js 抓住链接,把它粘贴到我的浏览器上就可以了。
  • 声明 debugger 也被忽略。在没有 nodemon 的情况下启动时,program(在 launch.js 中)必须设置为 typescript 文件,并且outFiles 必须指定到生成的 javascript 文件的映射。但是使用nodemon 启动时如何做到这一点?
  • @ManfredSteiner:您可以在下面看到我的答案...也许它会回答您的问题。但是在简历中,在您的 tsconfig.json 中,您必须将“outDir”选项设置为路径你想在哪里输出你的 js 文件(例如:“outDir”:“./build”)。然后,在您的 launch.json 中,将“outFiles”选项设置为与之前设置的路径相同的路径,但您必须使用 blob 正则表达式来选择它们(请参阅下面的配置)。此外,在您的 launch.json 中将“sourceMaps”选项设置为 true。希望对你有帮助
  • @TiagoBértolo:我不确定这是在 vscode IDE 中调试 typescript/javascript 代码的正确方法。这个想法是在 Visual Studio 代码中进行调试并在编辑器文件中到达断点。我不明白您的解决方案如何解决我的问题。但无论如何,如果您需要在 vscode 中调试和中断断点,您可以在下面看到我的答案。
  • @Philoufelin 我告诉你用 Chrome 开发工具调试你的应用程序,而不用 vscode。

标签: node.js typescript visual-studio-code breakpoints nodemon


【解决方案1】:

看来 vscode 有问题(Issue opens by on github [here][1])。但目前,解决方法是将配置(launch.json)中的协议设置为“inspector”。使用此选项,现在可以正确到达断点。

另外,将“runtimeArgs”选项中的 "--debug=5858" 更改为 "--inspect=5858"

{
  "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
},

另外,在那之后,如果你有一个闪烁的消息错误告诉你:

无法连接到运行时进程,10000 毫秒后超时 - (原因:无法连接到目标:连接 ECONNREFUSED 127.0.0.1:5858)

这意味着你的程序太短,调试器没有足够的时间在你的断点处中断。要解决此问题,请向选项“runtimeArgs”添加第二个运行时参数:"--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,则可以改为将主入口代码嵌入到 setTimeOut 函数中,并设置至少 1000 毫秒的超时时间。

所有这些选项都将为 vscode 提供足够的时间来中断您的断点。

://github.com/Microsoft/vscode/issues/23900“GitHub 问题”

【讨论】:

  • 注意 Deno 是 --inspect-brk
【解决方案2】:

@ManfredSteiner

我最近也遇到了这个问题。我猜你试图在你的入口文件(main.ts)的开头打破。我听说我们有这个错误消息,因为程序太短并且在调试器成功附加之前终止。您有 2 个解决方案:

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

  • 第二种解决方案是在您的 launch.json 中设置选项:“stopOnEntry”为“true”,并将 runtimeArgs 选项设置为 ["--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 毫秒)。希望对你有帮助

【讨论】:

  • 此设置有效。输出在选项卡 TERMINAL 中可见,并且程序执行在断点处正确中断。感谢支持。
【解决方案3】:

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

... 添加到文件 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
   }
 ]

但它不起作用。 10s 后 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 中尝试了这个链接,附件工作,但调试器语句或断点被忽略了。

我观察到使用 vscode 进行正常调试的进一步差异。正常调试在 DEBUG CONSOLE 选项卡中开始。在 TERMINAL 选项卡中启动 nodemon。

【讨论】:

    猜你喜欢
    • 2019-06-04
    • 2016-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-15
    • 1970-01-01
    • 2011-02-28
    • 2021-08-04
    相关资源
    最近更新 更多