what's the right process to debug with node-inspector? - javascript

I'm on node version v5.0.0. I installed node-inspector version: v0.12.5.
what I did:
1st. I start node-insepctor on port 8090 (due to 8080 is taken by nginx)
node-insepctor --web-port=8090
2nd. I start debugging my js file
node --debug-brk app.js
3rd. I open the url in browser
http://127.0.0.1:8090/?ws=127.0.0.1:8090&port=5858
It works for the 1st time, it will perfectly break at the first line. But after I find and fix the issue in my code and I want to debug again, it just can't works again.
What I tried:
1st.
I see that in the console, the following line which printed when I node --debug-brk app.js is still there:
Debugger listening on port 5858
So I refresh the page, the code is shown, but it just doesn't get executed, so no break point is reached.
2nd. I stop the app.js debug process by CTRL-c, and restart debugging app.js, Debugger listening on port 5858 is shown again. so I open the url again. same as above, the code is shown, but nothing happens.
3rd. I stop both the node-inspector process and app.js debug process and restart them both and open the url. this time, it works!!
Is there an easy way to debug a js program more than once??
Every time I need to kill both node-inspector process and debug js process to make it work again??
Or I'm doing something wrong here??
Please help!

node-inspector can be executed with the node-debug <file> command which adds the --debug-brk option by default. This opens the file in the Sources tab and breaks on the first line.
To continue debugging, you'll need to follow the steps below:
Select the file you need to debug from the Sources tab.
Click on a line number of the file to set a breakpoint.
Resume script execution (F8). This will cause the execution to break on the line that was highlighted in step 2.

Related

Debug a node.js process forked via child_process.fork in VSCode

I'm trying to debug a Node script forked using child_process's fork method using VSCode.
I have read almost all of the related docs, discussions but without any success. I'm not sure what am I doing wrong.
For your reference, I have posted a my sample code as a gist and is available at: https://gist.github.com/dpnishant/43096af3fecde4ec9fdea7c1d81cf543
It contains the three files: parent.js, child.js and launch.json (vscode configuration). I'm trying to setup 2 breakpoints: one at https://gist.github.com/dpnishant/43096af3fecde4ec9fdea7c1d81cf543#file-child-js-L4 and another at https://gist.github.com/dpnishant/43096af3fecde4ec9fdea7c1d81cf543#file-parent-js-L16.
Case-1
When I click "Start Debugging" using the "Launch Program" config, I'm able to hit the line 16 breakpoint on parent.js but the other breakpoint in child.js is never hit.
Case-2
When I click "Start Debugging" using the "Attach" config, I get the following error:
Debugging with legacy protocol because Node.js version could not be determined (Error: connect ECONNREFUSED 127.0.0.1:9999)
Case-3
When I start node parent.js in Terminal and immediately switch to VSCode to click "Start Debugging" using the "Attach to Process" config and choose the node process with the "child.js" script, I'm able to hit breakpoint on line 4 in child.js but by that time, the loop has already started and it's already late.
So my requirement is a method through which I can start my parent.js script and hit the desired breakpoint in child.js as soon as it starts execution.
Can someone point out to me where am I doing wrong? And what would be a optimum solution for my use-case?

How to initiate a new Node.js file after already initiating some other Node.js file on Windows 10 machine?

I've a laptop running on Windows 10 Home Single Language 64-bit Operating System
I am learning Node.js on this machine.
For downloading and installing Node.js I went to the URL
https://nodejs.org.
From there I downloaded the Node.js setup file v6.10.3 LTS for Windows (x64)
I started installing Node.js by double-clicking on the setup file and keep on clicking the Next button in the installation wizard.
Then, I clicked on the Windows Start button(the button appearing in bottom-left corner of the screen), two new options were appeared in the list of 'Recently Added' as Node.js and 'Node.js command prompt'
I clicked on the option 'Node.js command prompt', then following screen appeared.
Then, I created one file titled myfirst.js at location
C:\Users\lenovo\myfirst.js
Then, I initiated this file by typing in following text at the command prompt I opened earlier :
C:\Users\lenovo>node myfirst.js
Then, I run the file myfirst.js from the browser by hitting the URL :
http://localhost:8080/
Till now everything worked fine for me.
But real problem started when I wanted to run another program.
For it do I need to close the command prompt by clicking on the close button icon appearing on the top-right corner of the window and should I need to do this before running any new Node.js program to initiate the respective file?
The screen I'm having problem with is appears as below with the name of earlier run Node.js program(i.e. with the last initiated Node.js file) :
Someone please help me in this regard.
Thank you.
So, I'm not on Windows, but in general each node application will run in a single process by default. If you're running it from a command prompt, that will default to running the process in the foreground (meaning, it consumes the same process as your command window) and you have to open a new command window to run another program. You can either close that first window, or run the node application in the background.
You'll also want to make sure you're not trying to do two programs listening on the same port (i.e. two node apps both running on port 8088), as that's also not possible.
The node.js file you created is set up to listen to port 8080 and will do so until you "stop" the program from running. To do this, press CTRL-C. That will return you to the command prompt. To run your next program, initiate it with node filename.js.

How can i debug a nodejs backend with node-inspector?

I am trying to debug my backend and i am not really sure if i am doing it the correct way.
I have set up the debugging as follows:
Node 6.9.1 and node-inspector 0.12.8
open a command prompt and run the following command:
node-inspector --web-port=3030 (server app port)
open another command prompt and run the following command:
node --inspect --debug-brk server.js
browse to the given URL in the second command prompt log on screen
press F8 to make the server run
eventually put some others breakpoints
browse on another tab to your app (address and port defined in 1-)
see server execution stops on breakpoints defined on step 5.
Now when i run node --inspect app.js and everything looks good so far. I can debug the first start in the app.js. But if I want to debug an endpoint with POSTMAN, I get the error "Cannot POST /api/trainingsWeek". The endpoint works if I don't debug.
Do I have to take another address? Or another tool than POSTMAN?
ANd what is the difference between node --inspect app.js and node-debug
GitHub Issue
UPDATE
This was my stupid mistake :P Here is the solution: https://github.com/node-inspector/node-inspector/issues/907#issuecomment-280620108
I use VS Code to debug. I guess you don't need node-inspector, it will run with the integrated debugger from Node. To get it running, open your project and the debug view. There should be no configuration available. Click on the little wheel -> choose node.js as the environment.
After that the standard json will be created and looks like this:
And then click on the play button with the selection start programm. With that the debugging should run. Now you can use POSTMAN with the same url when you just run your server and the debugger from VS Code should hit your breakpoint.

How can I make node-inspector restart when node is restarted?

I use node-inspector a lot. When I edit my code and restart, I get the inevitable
Detached from the target
Error when a new process starts. I always have to go find the tab node inspector is on and restart it.
I was wondering if I could avoid this. For example, send a message to node-inspector from node to tell the browsers tab running node-inspector to restart.
You don't have to restart the Node Inspector process itself when the debugged process was restarted. All you need to do is reload the browser tab with Node Inspector GUI.
I am afraid there is no easy way at the moment for automatically reloading the Node Inspector GUI page when your debugged process is restarted. It is probably possible to perform some kind of active polling in Node Inspector backend, but that's a feature that would have to be implemented by somebody.
Depending on what part of your application you are debugging, you might find useful the feature "Live Edit". It allows you to edit your code from Node Inspector, save the changes to the Node/V8 runtime and possibly back to disk too. That way you don't have to restart the debugged process after you made your changes.
This feature has been implemented in Node Inspector and released in v0.7.0. See issue #266 for more details.
This feature has been implemented in Node Inspector and released in v0.7.0. See issue #266 for more details.
Previous answer here's a workaround:
I wrote a simple js script to be executed by greasemonkey/tampermonkey.
The script looks for the message "Detached from the target" on tab with address http://127.0.0.1:8080/debug?port=5858. Once the message is visible the page reloads until it disappears.
This solution is a workaround. It shouldn't be considered the ideal solution (I agree with Miroslav), here follows:
// ==UserScript==
// #name Reload node-inspector tab
// #version 0.1
// #description looks for the detached message and auto reload the page
// #match http://127.0.0.1:8080/debug?port=5858
// ==/UserScript==
var exec = function(){
setTimeout(function(){
var el = document.getElementsByClassName("help-window-title")[0];
if(el && el.innerHTML == "Detached from the target"){
location.reload();
} else {
setTimeout(function(){ exec(); }, 1000);
}
}, 1000);
};
exec();
Step1. don't use node-inspector - new work has been shipped by chrome team, which can't be integrated with node-inspector module. Moving forward you gonna miss those features if you stay with node-inspector.
Step2. To run your script use: nodemon --inspect-brk yourScript.js - the brk part creates an automatic break-point on the first line of code. If you don't have nodemon already installed, you do that first using: npm install -g nodemon.
Step3. Then open chrome app, open chrome dev tools (F12 or Ctrl+Shift+I) and click the node icon like so:
Step4. Make changes to yourScript.js - chrome devtools automagically reloads the debugger for every change you make in your project. This is because nodemon watches the project folder and resets the process - which resets the debugger connection.
More here: Debugging in 2017 with Node.js - i'm writing this in 29 august 2019 but 2 years later is still relevant.
Cross-posting slightly from this SO, with an update to this topic.
There is a link in Chrome (58) standard Developer Pane which opens a new "headless" window which reconnects magically to node inspect no matter how the app is rebuilt / restarted.
I'm running Express.js e.g. DEBUG=myapp:* supervisor -- --inspect bin/www & and found it difficult to reconnect using the normal guid-laden URL which keeps changing. But this Chrome tool works all day reconnecting reliably.
Under Threads > Main, you should see "Node instance available. Connect".
I find the new-window less usable as I'd prefer a tab, but the auto-reconnect is so reliable I'll live with that!
The only downside I've found is when it does reconnect it clears all breakpoints.
Sure, it's easy. First install npm install -g nodemon
Then you can run node-inspector & nodemon --debug app.js
(replacing app.js with the name of your script)
Though on syntax error you still may need to reload node-inspector tab manually

Failing to debug with node-inspector - what is wrong?

I have a javascript code that checks whether a point is inside a polygon. I am trying to debug it with node-inspector, following these steps:
In the first window:
PS Z:\dev> node-inspector.cmd
info - socket.io started
visit http://0.0.0.0:8080/debug?port=5858 to start debugging
In the second window:
PS Z:\dev\poc\SDR> node --debug-brk .\IsPointInside.js
debugger listening on port 5858
Now I navigate to http://localhost:8080/debug?port=5858 in my Chrome browser.
What happens is that Chrome gets stuck waiting for localhost presenting me the empty screen.
I must add that I have successfully debugged the r.js javascript optimizer using the same steps before, but now I cannot debug it as well.
according to this there seems to be an order in which the components should be started.
Its application -> debugger -> web browser.
Could be your issue.
You can also send a "debug on demand" command. Use this command:
kill -USR1 $PROCESSID

Categories