Debugging Node/Express --- require('express') creates a break

17,689

Solution 1

There is a popular GUI debugger (leveraging WebKit, i.e. Chrome, Safari..).

You should give it a go https://github.com/dannycoates/node-inspector

Solution 2

Just echoing the call for Node Inspector with a bit more explicitness.

$ sudo npm install -g node-inspector
$ node-debug app.js <arguments to your app>
debugger listening on port 5858
Node Inspector is now available from http://localhost:8080/debug?port=5858
Debugging `app.js`

Now in (a webkit-based?) browser go to that address - or it works just fine remotely too, as long as the ports are open.

enter image description here

Now you have full access to all the server side variables (the app object for instance), exposed to the client side debugger. It's quite magical. You can set breakpoints at app.get() entry points, or at server init, or wherever.

Share:
17,689
Gregory M
Author by

Gregory M

Updated on July 04, 2022

Comments

  • Gregory M
    Gregory M almost 2 years

    My fairly limited experience with Node and Express doesn't help, but I'm having trouble debugging the app from the terminal window. Here is the issue:

    running node debug app.js returns:

    < debugger listening on port 5858
    connecting... ok
    break in server.js:1
        1 var express = require('express');
    

    Is this the right way of debugging express apps?

  • Gregory M
    Gregory M about 12 years
    node-inspector seems like the way to go, thanks. What it still not clear to me though is how you access the server variables. In this example for instance, express returns undefined.
  • 250R
    250R about 12 years
    I assume you want to inspect variable using built-in debugger? Try having a look at the example here nodejs.org/api/debugger.html Basically you need to get into REPL mode, then inspect..
  • Cole Peterson
    Cole Peterson almost 10 years
    I cannot get this to work. node inspector open up, but no app.js to be found.
  • jebbie
    jebbie over 8 years
    this one should be the accepted answer - just starting up "node-inspector app.js" won't do the trick at all ;) and.. just leaving a link with "hey, check this out" is just not what feels like an answer to me.. ^^
  • jebbie
    jebbie over 8 years
    Add the right command how to start it up, not just leave a link... and earn my upvote... ^^