简体   繁体   English

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

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

I am running my JS using 'node myscript.js'.我正在使用“node myscript.js”运行我的 JS。

And I try to debug this in VSCode using 'Run->Start Debugging'我尝试在 VSCode 中使用“运行->开始调试”进行调试

I create a workspace in VSCode and when I see it create a launch.js我在 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}"
        }
    ]
}

But the debugger fails with但是调试器失败了

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

how can i get this to work?我怎样才能让它工作?

I have tried all these configuration.我已经尝试了所有这些配置。 But none of them hit the breakpoints I set.但是它们都没有达到我设置的断点。 I do see the console output from my Console.log in the terminal.我确实在终端的 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}"
        }
    ]

Using the pwa-chrome type for launching will try and launch your code in a web browser and run it, that doesn't run node at all so seems to be the wrong approach for your needs and hitting break points that way is more complex.使用pwa-chrome类型启动将尝试在 web 浏览器中启动您的代码并运行它,这根本不运行node ,因此似乎是满足您需求的错误方法,并且以这种方式达到断点更复杂。 I would suggest going with a simple pwa-node type of launch instead as hitting break points is nice and easy.我建议使用简单的pwa-node类型的启动来代替,因为打断点既好又容易。

Your first step is to read through the node debugging instructions for vscode: https://code.visualstudio.com/docs/nodejs/nodejs-debugging第一步是通读 vscode 的节点调试说明: https://code.visualstudio.com/docs/nodejs/nodejs-debugging

In the above instructions it suggests using the launch type node but more recent instructions suggest using pwa-node (see here: What is the pwa-node type launch configuration on VSCode? ).在上面的说明中,它建议使用启动类型node ,但最近的说明建议使用pwa-node (请参阅此处: VSCode 上的 pwa-node 类型启动配置是什么? )。

A simple .vscode/launch.json file to debug a node application that is launched via index.js :一个简单的.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",
      ]
    }
  ]
}

Once you have the launcher you can set a break point in your js and then hit the debug button.一旦你有了启动器,你可以在你的 js 中设置一个断点,然后点击调试按钮。

Details of the launch configuration attributes can be found here: https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_launch-configuration-attributes可以在此处找到启动配置属性的详细信息: 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