VSCode debugging not working for NodeJs application - node.js

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?

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):
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.
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:
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.
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.

Your breakpoints are probably set too early and are not registered by node. It should help if you set the breakpoints after you have attached.
We have improved this experience in VSCode and it should be available in 0.4.0

Always clear your breakpoints and set them after you attach. I learned the hard way. This is certainly a bug.
I've been digging into this and here is what I have found so far for 0.3.0.
This does not work!
In Code add a breakpoint to app.js or a route
In terminal run node --debug src/server/app.js
In Code attach the debugger
This works!
In terminal run node --debug src/server/app.js
In Code remove all breakpoints
In Code add a breakpoint to app.js or a route
In Code attach the debugger
This does not work, as --debug doesn't kick in unless its the arg after node and before the file
In terminal run node src/server/app.js --debug
In Code remove all breakpoints
In Code add a breakpoint to app.js or a route
In Code attach the debugger
This works, assuming you have a gulp process
In terminal run gulp serve-dev --debug
In Code remove all breakpoints
In Code add a breakpoint to app.js or a route
In Code attach the debugger
This does not work, sometimes
In terminal run gulp serve-dev --debug
In Code add a breakpoint to app.js or a route
In Code attach the debugger
Why sometimes? The best I can tell is that that the breakpoints sometimes get funky. Sometimes they work fine, and other times I have to remove them and re-add them before attaching the debugger.

Related

Is it possible to debug node js code that runs before the debugger attaches?

Using the instructions found here I have setup Visual Studio Code to debug my nodejs code.
But it takes the debugger a few seconds to attach to node. And during that few seconds the code just runs.
So a file like this, with a breakpoint on line 1:
• 1 console.log('')
Will never break because it quits before the debugger attaches.
I can quickly ^c the code then restart and sometimes catch the debugger but this is unreliable.
Is there an event I can wait for in my code so that I know the debugger is attached and it is safe to continue?
Or is there a better configuration for doing this?
Yes, using the --inspect-brk option for node instead of --inspect.
so when running your code it would look something like this
node --inspect-brk server.js

Node Inspector not hitting breakpoint for Intern test

I am trying to debug my intern test using node inspector. I followed all the steps correctly from here. I gave it a run by performing the following command
Start Selenium[for intern test]
Run the intern test in node environment
C:\node\node --debug-brk node_modules/intern/runner.js config=tests/intern
In another cmd I run the node inspector
node-inspector --web-port=9999 &
Start the browser
http://127.0.0.1:9999/?ws=127.0.0.1:9999&port=5858
I can see all the script of my project. I have break point set in one of the test component. But the moment I resume the debugger the test start running but the breakpoint is never hit. What am I doing wrong ?
I think you should add debugger; into your script as a break point

Webstorm debugger breakpoints doesn't work in nodejs

When I try to set a breakpoint and debug the file, the debugger just run the app without stopping at breakpoints.
This is my code:
console.log('123');
And this is my output:
/usr/bin/node --debug-brk=39765 --nolazy index.js
debugger listening on port 39765
123
Process finished with exit code 0
Does anyone has an idea what could be the problem?
Try to disable js.debugger.v8.use.any.breakpoint in WebStorm registry.
You can do it by going to Help -> Find Action
In there just enter Registry.
For me disabling this option made debugging anything node-related much faster and much more predictable.
Wanted to chime in and say that it is absolutely critical that you use --debug-brk and not --debug with getting WebStorm breakpoints to work for remote debugging as well as running the server directly from webstorm.
Even though --debug-brk technically just stops and waits for the debugger to join, and --debug allows you to join later, my breakpoints failed with just --debug no matter the remote configuration I tried.
As far as I can tell, connecting WebStorm 11 to a node.js server on the debug port, with only --debug, will connect but fail to load any breakpoints that work.
disabling js.debugger.use.node.options in the webstorm registry helped me
https://youtrack.jetbrains.com/issue/WEB-47774#focus=Comments-27-4436526.0-0
Debug breakpoints not work in my PhpStorm 2016.3.2 with NodeJS 7.7.x. My expectation is, that WebStorm will have same issue.
If you downgrade to Node 6 (I tested with 6.9.4), it starts working correctly.
For me the issue was that WebStorm didn't play well with my typescripts. It would say debugger listening and then run through the entire program without stopping at the breakpoint as described in the question.
The workaround I used was to simply put the breakpoint in compiled js file and debug from there instead.
Go to Run -> View Breakpoints... or hit
shift+command+F8 in OS X.
Select your breakpoint from the list and make sure Suspend is checked.
Fixed the issue by enabling generation of 'map' file in the TypeScript configuration.
Add '--sourceMap' in Tools-->Languages & Frameworks-->TypeScript-->Options
I'm having the same issue in version 2022.2 but I don't think it is a problem with the version
Here are the potential solutions;
Go to;
WebStorm -> Preference -> Build, Execution, Deployment -> Debugger
Click "Allow unsigned requests" checkbox and make it checked
This solved my issue.
Or
Check your local environment. What I mean by saying check your local environment is usually more than one service is trying to use the same debugger port if we speak about the e.g. nodejs.
You will get the following message if you try to run two services at the same time in the debug mode;
Starting inspector on 127.0.0.1:9229 failed: address already in use
Try to start the main service first that you try to debug.

Node-inspector not init breakpoints

I run node server in console
Then in browser :8080/debug?port=5858
i see
My breakpoint doesnt work .
Help me please.
You need to run node-inspector after your app.
Also, in your case, it seems by the time you start inspector, the app will be past the section you want to debug. So try this:
# Console window 1
node --debug-brk run.js
# Console window 2
node-inspector
Then navigate to http://localhost:8080/debug?port=5858
The execution should be stopped on the first line of code. Set up your breakpoints and resume.

nodejs start debugger on different port

Im a little confused with all this nodejs debug syntax flying around.
I simply want to start the debugger on a process when I run it on a different port.
Normally I start debugging by node debug file.js
but now I have to process` running that I need to debug
Now I found the command node --debugger=7873 file.js but that starts the debugger and jumps past all the breaks and I tried node --debugger=7837 --debug-brk file.js but that forces me to consume another teminal window. How can I just run a script on a different port in the same terminal or with out using nohup?
node debug --port=[your port] your_program.js
responsible _debugger code here

Resources