首页 > 解决方案 > VSCode 将调试附加到在 WSL 上运行的 nodejs

问题描述

我收到错误:无法连接到运行时进程,10000 毫秒后超时 - (原因:无法连接到目标)

NodeJs v4.3.1 在 WSL 上运行。

下面是我的launch.json

"type": "node",
"request": "attach",
"name": "Attach to WSL",
"port": 3000,
"address": "localhost",
"restart": true,
"protocol": "inspector",
"localRoot": "${workspaceFolder}/web-frontend",
"remoteRoot": "/mnt/c/workspace/.../web-frontend"

WSL 使用来自 Windows 的路径,因此 localRoot 和 remoteRoot 是相同的。

到目前为止我错过了什么?

标签: node.jsvisual-studio-code

解决方案


我创建了一个 VS Code 扩展:WSL workspaceFolder,它将帮助您自动将remoteRootlaunch.json 中的设置为正确的 WSL 路径。例如 my launch.json,根据您的文件路径,如下所示;

/vscode/launch.json

{ // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "type": "node", "request": "attach", "name": "Attach to Remote", "address": "localhost", "port": 5858, "localRoot": "${workspaceFolder}/web-frontend", "remoteRoot": "${command:extension.vscode-wsl-workspaceFolder}/web-frontend" } ] }

然后启动调试会话,在 WSL 终端中输入NODE_ENV=debug node --nolazy --inspect-brk=5858要调试的脚本的路径之前。

但是,您可能会在使用 Node v4 时遇到问题,因为它不支持“检查器协议”。我强烈建议您升级到更新版本的 Node.js。目前 v8 是最新的 LTS 版本:Node.js 发布


推荐阅读