Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VSCode debugging not working for NodeJs application

I have added the configuration in the launch.json file with the following details :

{
    "name": "Attach"
    "type": "node",
    // TCP/IP address. Default is "localhost".
    "address": "localhost",
    // Port to attach to.
    "port": 5858
}

Now I start my app with the following command : node --debug-brk ./bin/www

When i go to VSCode and select Attach in the debugging menu on the top and click on the play button. It attaches, but when i go to browser and open a page, it doesn't it the breakpoint or the handler function in my index.js file. Can you please help what could be going wrong?

like image 365
sumit agarwal Avatar asked Jun 05 '15 09:06

sumit agarwal


People also ask

Why my debugger is not working in VS Code?

The most common problem is that you did not set up launch.json or there is a syntax error in that file. Alternatively, you might need to open a folder, since no-folder debugging does not support launch configurations.

How do I debug Node scripts in VS Code?

There are a few ways you can debug your Node.js programs in VS Code: Use auto attach to debug processes you run in VS Code's integrated terminal. Use the JavaScript debug terminal, similar to using the integrated terminal. Use a launch config to start your program, or attach to a process launched outside of VS Code.

How do I debug Node in Visual Studio?

The easiest way to start a debugging session in Visual Studio Code is to open a file in the editor, click the Run View icon in the Activity Bar (or press Ctrl+Shift+D on your keyboard), followed by the Run and Debug button at the top left corner of the application.


1 Answers

There are two issues with breakpoints in node (and these issues are not specific to VSCode but you can see them in node-inspector as well):

  1. If you set breakpoints in your app's startup code and launch node with --debug (opposed to --debug-brk), node starts immediately and has executed your startup code before VSCode had a chance to register the breakpoints. So if you need to debug startup code use the --debug-brk flag because it allows VSCode to set breakpoints before node starts the application.

  2. Node does not parse source files completely on load but delays parsing of closures (callbacks etc.) until their code is first hit. As a consequence breakpoints set on callbacks are not always correctly registered by node, because it does not have parsed the code yet. This 'lazy' behaviour can be disabled by starting node with the --nolazy flag.

In the next version of VSCode (0.4.0) we try to address these problem as follows:

  1. VScode will always launch node with the --debug-brk flag but will hide the first stop and continue if the user did not specify "stopOnEntry: true". This will avoid problems with missed breakpoints in startup code.

  2. If breakpoints are set in code that has not been parsed by node, node will register them at the next possible position in parsed code. Since these "actual" positions are returned by node to the client, VSCode is able to show these positions. So the user will see that a breakpoint set in an unparsed callback "jumps" to a position further down and he will better understand why the debugger is not stopping at the location requested. In addition we added a "Reapply" button to the breakpoint view which makes it really easy to clear and set all breakpoints.

like image 119
Andre Weinand Avatar answered Nov 07 '22 15:11

Andre Weinand