How to really debug node.js app? - node.js

I installed node-inspector.
I run it: node-debug app.js
But it opens the empty browser.
If I try to run node.js in debug mode before: node --debug app.js
It kind opens my scripts in browser.
But it's still not clear how to make it work. What to do next? Which url to use to see the working app itself in browser and trigger app to go to breakpoints?
My express app is usually running under localhost:3000. Should I run this url? It's not working.
Thanks

Okay,
I was able to figure it out. The problem was my app is Express based and it's a little bit different. For some reason it's not easy to find these differences until you start digging into every details. There is the instructions how to run debugger if you app is working under Express.
Prerequisites:
Windows 7
node.js app based on Express
Now:
Step 1. Run app in the debug mode (staying in the folder where your app.js located in the first command prompt), do either of 2: node --debug app.js or node --debug bin/www.
Step 2. Open the second command prompt and run node-inspector bin/www (or node-inspector app.js)
If you see the error that it can't access port 8080 it means that something is already listening to it. So you need to run this command differently: node-inspector --web-port=8099 bin/www
After step 1 you will able to run your app as usually in browser: localhost:3000.
After step 2 you open another browser window and enter: http://127.0.0.1:8099/?port=5858

Related

Unable to start the nodeapp through terminal

I have a node app locally and I normally run it using the run button in the web storm, which works fine. While I am documenting the project I came across this blocker: if I try running the same app through the terminal using the command: node app.js , the server starts but the browser throws and error stating "This site can’t be reached" "localhost refused to connect".
Because the port is occupied.
You can change it using app.listen({port})...
or
linux:
export PORT=4500
node server.js
windows:
set PORT=4500
node server.js
I found this myself.. since I used the scaffolding app and it stores info related to server at bin/www, so we will not be able to run the app using the command : node app.js but instead we could run the app using nodemon(which I installed globally on my machine)
Love nodemon for this awesome feature <3 :)

Debugging Node/Express RESTful API with node-inspector

I have seen many question about debugging a Node/Express application, but, it seem a node web application, not a RESTful server.
I have built a simple server, and it work perfect when start server with command node server.js. I can use Postman, or some other tools to invoke a GET/POST request.
After installing the node-inspector module, when I start a node-inspector debug with command node-debug server.js, it show me a webpage at http://127.0.0.1:8080/?ws=127.0.0.1:8080&port=5858 with inspector window.
Then, I use postman to invoke request again (which has invoked successfully earlier), but it show me a 404 error: Cannot GET /api/v1/user/login.
How can make a request to debugging server?
Here is my solution: Do not use node-debug *.js directly. Instead, open two terminals in your project. For example, I want to debug server-3.js, like the pictures down here. In one terminal, use node-inspector for debugging listening, and another use node --debug server-3.js
after sending a request on postman:
you can see your debugging situation in your node-inspector window listening on port 5858:
I believe this is what you want to achieve.
The latest update is that you can also resort to vscode, which has a plugin called "Debugger for Chrome" that has the debug function you want.
Try with nodedev
1) Install nodedev:
sudo npm install -g nodedev
2) Start your server:
nodedev server.js
3) Visit the page http://127.0.0.1:7001/debug?port=7000 to debug...
4) ...
5) Profit!
Bonus: Your server will automatically restart when changes are detected!
Use node-inspector instead of node-debug (comes in the same module)
In one terminal execute:
node-inspector
In another terminal execute
node --debug server.js
Note that --debug option is deprecated. --inspect is preferred. Otherwise you will get a warning/error.
(node:11524) [DEP0062] DeprecationWarning: node --debug and node --debug-brk are invalid. Please use node --inspect or node --inspect-brk instead.

Debugging script using node-inspector is not working

I am trying to debug a very simple script using "node-inspector".
I tried both following instructions on the repo, which include running "node-debug" and instructions here which include running "node --debug-brk yourApp.js".
The main problem is that neither of the commands "node", "node-debug" or "node-inspector" return any result. They just return silently.
Running "nodejs --debug-brk myScript.js" on the other hand works, but does not seem to have a nice debug GUI. I can connect to it on http://127.0.0.1:5858/ but it is hardly useful for variable inspection.
Once you've installed node-inspector globally (with npm install -g node-inspector) you can use it to connect to a nodejs process that's been run in debug mode. Try the following steps.
Open two terminal windows
In the first, run your process you'd like to debug with node --debug-brk myscript.js
In the second, run node inspector with node-inspector
In Chrome, visit the following address: http://localhost:8080/debug?port=5858
What you've done here is start node in debug mode (at port 5858 by default), then launch node inspector, which runs its own webserver on port 8080. The URL (the debug?port=5858 part) is telling node-inspector to connect to the node debug process that's on port 5858. Once you're in there, you'll see that your process has stopped on the first line (as instructed to by --debug-brk). You can then set any other breakpoints you'd like, then press the "play" button to start your process running!

Cannot get node-inspector working

I have a basic express app that I am trying to debug but I cannot get node inspector working correctly on OSX
In one terminal window I run:
node --debug-brk server.js
In another terminal window I run:
node-inspector
Node Inspector opens here:
http://localhost:8080/debug?port=5858
In another browser tab I try to open my site here
http://localhost:8080/
I get printed to screen
Cannot GET /
If I open instead
http://localhost:3000
I get my app because I set this in server.js
app.set('port', process.env.PORT || 3000);
But now none of the breakpoints work in the debugger
I am sure I am doing something stupid here but I have read the docs and tutorials over and over and I cannot get this to work.
The other things I have tried is in one terminal window running:
node-debug server.js
I get the same thing as above. Debug inspector opens and is stopped at first line and my site at
http://localhost:8080
still says
Cannot GET /
I also tried
node-debug --no-debug-brk server.js
An I get the same result except node inspector in not stopped at first line
Debug inspector opens and is stopped at first line...
When node-inspector starts debugging an application, by default, it stops at the first line, during the app starting process. If you try to access your app at http://localhost:3000 with the debugger stopped at the first line, you'll never get to your breakpoints.
Before trying anything, you must resume script execution or start node-inspector with the --no-debug-brk option, to prevent stopping at the first line.
To make sure node-inspector is not stopping at any line at all, what you should do:
put a breakpoint at the line where you have your app.listen call, or;
a more extreme solution: put a breakpoint at every line of your main file.

debugging node.js with node-inspector

I'm trying to debug my nodejs app using node-inspector. But Google Chrome doesn't show the code.
I'm using the following,
Node.js : v0.10.26
Express : 4.0.0
Node Inspector : v0.7.3
Google Chrome version : 34.0.1847.131
This is what I'm doing to start the debugger..
$ node-inspector
Node Inspector v0.7.3
Visit http://127.0.0.1:8080/debug?port=5858 to start debugging.
In another console,
$ node --debug app.js
debugger listening on port 5858
$
Then started Google Chrome and went to
http://127.0.0.1:8080/debug?port=5858
It opens up node-inspector but without any code..all windows are empty.
Noticed that I'm not getting 'Express server listening on port 3000'
Tried all as per node-inspector fails to connect to node but no luck
Couldn't work out what I'm missing. Would be great of you have any suggestions..so I can debug my Node.js apps in Google Chrome.
Try to run node --debug-brk app.js instead of just --debug. Your application may not be pausing before node inspector hooks into the node process. Using --debug-brk will force node to break on the first line of your app and wait for a debugger to attach to the process. Loading the node-inspector web interface is what causes node-inspector to attach to your node process; that's why you include the node debug port in the query string (localhost:8080/debug?port=5858). You're telling node-inspector what port it should reach out and attach to.
Here's an animated gif I put together showing a complete install and run of node-inspector.
In the gif I use the --debug flag because I'm not debugging any code that runs right at startup. I'm debugging inside a request handler, which only fires when the page is requested. Thus, refreshing the page causes node-inspector to break on that line.
I also put together a 15 minute YouTube tutorial a while ago.
http://youtu.be/03qGA-GJXjI
node-inspector by default tries to pre-load all the code before initiating the debug window. I have had instances, node-inspector just hangs for ever because of this pre-loading. Luckily the newer versions have an option to stop the pre-load thereby making the inspector load faster.
Try node-inspector --no-preload
Standard remote debugging is broken entirely in node 6.5. It's replaced however by a new internal node feature
$ node --inspect --debug-brk build/server/server.js
Debugger listening on port 9229.
Warning: This is an experimental feature and could change at any time.
To start debugging, open the following URL in Chrome:
chrome-devtools://devtools/remote/serve_file/#62cd277117e6f8ec53e31b1be58290a6f7ab42ef/inspector.html?experiments=true&v8only=true&ws=localhost:9229/node
Debugger attached.
See here - http://arveknudsen.com/?p=346%3Fpage_id%3D346&print=pdf - for more info
--debug-brk is now deprecated
try node --inspect-brk <your starting file name>
and then go to chrome and type url
chrome://inspect and click on Open dedicated DevTools for Node,
the debugger will start, no need of node-inspector
On the left of Node Inspector, "Sources" tab, there is "a box with a triangle in it" - highlighting says "Show Navigator". (See it in the picture above). Open that to find the files you want to debug, and put a break point on code that has yet to run.
Also note, if you want to debug code that runs on starting node, you'll need to use the --debug-brk option when starting. Then, in Node Inspector, you you'll have to kick off the app (F8 to run all). You'll need this option if you want to debug all the initialization code, like starting a web browser.
node-debug --no-preload app.js
This what's working for me. Accoriding to this:
My script runs too fast to attach the debugger.
The debugged process must be started with --debug-brk, this way the
script is paused on the first line.
Note: node-debug adds this option for you by default.

Resources