简体   繁体   中英

Debugging in Visual Studio Code with Windows Subsystem for Linux

I'm trying to setup a debug configuration for running tests in Node app using Mocha. I have Ubuntu running as a Windows Subsystem for Linux and my Visual Studio Code is setup to use Bash as the integrated terminal.

Here is my launch.json configuration:

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

    ...
}

This will run my tests in the integrated terminal, but will always time out when trying to attach to the debugger, with the following error:

错误信息

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

Also here is what it shows in the terminal before it starts running the tests:

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

Any idea how to make sure it connects the debugger?

You have to configure the npm script, too.

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM