4

I am running my JS using 'node myscript.js'.

And I try to debug this in VSCode using 'Run->Start Debugging'

I create a workspace in VSCode and when I see it create a 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.

{
    // 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}"
        }
    ]
n179911
  • 19,547
  • 46
  • 120
  • 162
  • `I am running my JS using 'node myscript.js'` You are running it manually and then launching it with a launch config? If so, any particular reason? Is the port you are running the node.js app in conflicting with `:8080`? If you are running it manually you can use attach instead of launch – soulshined Jul 20 '20 at 22:41
  • I just want to debug a script in VS code. I normally run it via command prompt 'node myScript.js'. When I debug in VS code, it asks me for a launch config. I don't know why. – n179911 Jul 21 '20 at 04:22
  • Oh yeah that’s just how vscode works, if you want to use their debugger. Review their guide it’s got a lot of healthy info https://code.visualstudio.com/docs/nodejs/nodejs-debugging. And I’m almost certain this would be a duplicate question just on mobile so can’t look. – soulshined Jul 21 '20 at 04:31
  • Thanks for your help. I did read that article. And I have tried 4 configruation , but none of them hit the breakpoint in the JS file I set. – n179911 Jul 21 '20 at 04:52
  • And I want to add that when I run my script 'node myscript.js', it is not listening on anyport. I just run the js and exit when the script is done (take < 1 min) – n179911 Jul 21 '20 at 04:57
  • Do you have a script called run-script or debug? That’s just boilerplate for their example I’m pretty sure. You should include the name of the script you want to run – soulshined Jul 21 '20 at 05:01

1 Answers1

0

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. I would suggest going with a simple pwa-node type of launch instead as hitting break points is nice and easy.

Your first step is to read through the node debugging instructions for 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?).

A simple .vscode/launch.json file to debug a node application that is launched via 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.

Details of the launch configuration attributes can be found here: https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_launch-configuration-attributes

Oly Dungey
  • 1,603
  • 19
  • 20