繁体   English   中英

如何在 VSCode 中调试运行“节点”的 javascript

[英]How to debug in VSCode for javascript running 'node'

我正在使用“node myscript.js”运行我的 JS。

我尝试在 VSCode 中使用“运行->开始调试”进行调试

我在 VSCode 中创建了一个工作区,当我看到它时创建了一个 launch.js

{
    // 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": "pwa-chrome",
            "request": "launch",
            "name": "Launch Chrome against localhost",
            "url": "http://localhost:8080",
            "webRoot": "${workspaceFolder}"
        }
    ]
}

但是调试器失败了

Debugger listening on ws://127.0.0.1:55596/0b6425a4-e21c-47c2-a81d-bb8e43386246
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
Waiting for the debugger to disconnect...
Process exited with code 0

我怎样才能让它工作?

我已经尝试了所有这些配置。 但是它们都没有达到我设置的断点。 我确实在终端的 Console.log 中看到了控制台 output。

{
    // 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": [
        {
            "name": "Attach by Process ID",
            "processId": "${command:PickProcess}",
            "request": "attach",
            "skipFiles": [
                "<node_internals>/**"
            ],
            "type": "pwa-node"
        },
        {
            "name": "Launch via NPM",
            "request": "launch",
            "runtimeArgs": [
                "run-script",
                "debug"
            ],
            "runtimeExecutable": "npm",
            "skipFiles": [
                "<node_internals>/**"
            ],
            "type": "pwa-node"
        },
        {
            "command": "npm start",
            "name": "Run npm start",
            "request": "launch",
            "type": "node-terminal"
        },
        {
            "type": "pwa-chrome",
            "request": "launch",
            "name": "Launch Chrome against localhost",
            "url": "http://localhost:8080",
            "webRoot": "${workspaceFolder}"
        }
    ]

使用pwa-chrome类型启动将尝试在 web 浏览器中启动您的代码并运行它,这根本不运行node ,因此似乎是满足您需求的错误方法,并且以这种方式达到断点更复杂。 我建议使用简单的pwa-node类型的启动来代替,因为打断点既好又容易。

第一步是通读 vscode 的节点调试说明: https://code.visualstudio.com/docs/nodejs/nodejs-debugging

在上面的说明中,它建议使用启动类型node ,但最近的说明建议使用pwa-node (请参阅此处: VSCode 上的 pwa-node 类型启动配置是什么? )。

一个简单的.vscode/launch.json文件,用于调试通过index.js启动的节点应用程序:

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "pwa-node",
      "request": "launch",
      "name": "Launch my Node programme",
      "skipFiles": ["<node_internals>/**"],
      "cwd": "${workspaceFolder}",
      "program": "index.js",
      "args": [
        "command-line-arguments",
        "go-here",
      ]
    }
  ]
}

一旦你有了启动器,你可以在你的 js 中设置一个断点,然后点击调试按钮。

可以在此处找到启动配置属性的详细信息: https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_launch-configuration-attributes

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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