【问题标题】:Debugging in Visual Studio Code with Windows Subsystem for Linux使用适用于 Linux 的 Windows 子系统在 Visual Studio Code 中进行调试
【发布时间】:2017-08-01 01:30:36
【问题描述】:

我正在尝试设置调试配置,以便使用 Mocha 在 Node 应用程序中运行测试。 我有 Ubuntu 作为 Linux 的 Windows 子系统运行,我的 Visual Studio Code 设置为使用 Bash 作为集成终端。

这是我的launch.json 配置:

{
    "name": "Launch via NPM",
    "type": "node",
    "request": "launch",
    "console": "integratedTerminal",
    "cwd": "${workspaceRoot}",
    "runtimeExecutable": "npm",
    "runtimeArgs": [
        "test"
    ],
    "protocol": "inspector",
    "port": 5858

    ...
}

这将在集成终端中运行我的测试,但在尝试附加到调试器时总是会超时,并出现以下错误:

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

这也是它在开始运行测试之前在终端中显示的内容:

cd c:\Users\abc\MyProject ; npm test
-bash: cd: c:UsersabcMyProject : No such file or directory

知道如何确保它连接调试器吗?

【问题讨论】:

  • 换个端口有用吗?尝试设置diagnosticLogging: true并查看日志。
  • 尝试了不同的端口。也不允许使用diagnosticLogging 属性

标签: node.js visual-studio-code windows-subsystem-for-linux


【解决方案1】:

你也必须配置 npm 脚本。

package.json

{
  "name": "xy-controller",
  "version": "1.0.0",
  "main": "./main.js",
  "scripts": {
    "debug": "node --inspect=5859 main.js"
  }
}

launch.json

{
    "type": "node",
    "request": "launch",
    "name": "Launch Program via NPM",
    "cwd": "${workspaceRoot}",
    "runtimeExecutable": "npm",
    "runtimeArgs": [
        "run-script", "debug"
    ],
    "protocol": "inspector",
    "port": 5859
}

【讨论】:

  • 这是个好主意,但是当我尝试连接到进程时它会超时。调试控制台显示:C:\Program Files\nodejs\npm.cmd run-script debugDebugger listening on ws://127.0.0.1:5858/7e740d3b-d874...
  • 如果我尝试添加 "console": "integratedTerminal",(即 bash),它会与路径混淆:cd "c:\Users\...." ; "C:\Program Files\nodejs\npm.cmd" run-script debug -bash: cd: c:\Users\...: No such file or directory C:\Program Files\nodejs\npm.cmd: command not found
猜你喜欢
  • 2019-05-16
  • 1970-01-01
  • 1970-01-01
  • 2022-11-10
  • 2019-10-31
  • 2018-11-27
  • 1970-01-01
  • 2016-11-28
  • 1970-01-01
相关资源
最近更新 更多