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

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

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

大家好。

当使用 nodemon 启动它时,我正在尝试使用 visual studio 代码在服务器端调试 typescript 代码(或 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"]
    }

我在 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 服务器按预期重新启动。

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

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

谢谢你的帮助

最佳答案

看起来 vscode 中有一个问题(Issue opened by on github [here][1])。但目前,解决方法是将配置 (launch.json) 中的协议(protocol)设置为“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
},

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

Cannot connect to runtime process, timeout after 10000 ms - (reason: Cannot connect to the target: connect 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,则可以将主入口代码嵌入到至少有 1000 毫秒超时的 setTimeOut 函数中。

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

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

关于node.js - vscode : Can't break on any breakpoint with Visual Studio code when launching with nodemon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43217441/

相关文章:

javascript - 在nestjs中为不同位置和前缀提供多个静态文件?

javascript - 更改 Node js 上 ChartJS 的背景

node.js - 触发配置单元查询并在UI上显示增量数据

reactjs - Material UI Chip数组如何像Angular Chip Input一样使用?

c - c99 模式下的 VScode "expression must have a constant value"

node.js - Sequelize 不同步 - 插入

javascript - 选择了哪个垫菜单?

javascript - Typescript 有静态导入吗?

Visual Studio Code 中的 Java 编译错误 : Could not find or load main class

reactjs - npm start 命令不起作用,当我尝试运行我的 React 应用程序时