69

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.

Community
  • 1
  • 1
genwip
  • 1,027
  • 1
  • 12
  • 20

6 Answers6

122

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

starball
  • 20,030
  • 7
  • 43
  • 238
CatDadCode
  • 58,507
  • 61
  • 212
  • 318
  • 1
    Thanks Alex, I did check your youtube video before..very informative thanks. --debug-brk did the trick for me..however..when I put a breakpoint in one of my routes. In my case, I'm experimenting with cheerio + request to scrape a website. I've created a route, lets say scrape.js Im hoping to debug this file..but its not hitting the breakpoint..am I missing something? – genwip May 01 '14 at 14:14
  • I'm not sure. It's hard to know without a code sample. Can you paste all the code that runs up to the point where that route is declared in a [gist](http://gist.github.com) along with the URL you're navigating to to trigger the request handler? – CatDadCode May 01 '14 at 16:06
  • 1
    @AlexFord: How did you manage to create this gif? I think its really cool & can be very helpful – SharpCoder Feb 12 '15 at 10:07
  • @SharpCoder I used [Camtasia](http://www.techsmith.com/camtasia.html) to record a video. In their advanced export options you can choose animated gif. – CatDadCode Feb 24 '15 at 03:19
  • I want to invoke mocha unit test along with node-debug " >node-debug -p 8900 mocha . " , but facing similar problem, if we try to invoke node-debug with mocha, how to put -brk switch as we don't get handle on node – Anand Mar 20 '15 at 09:04
  • 1
    @SutikshanDubey refer to [this answer](http://stackoverflow.com/a/15884692/498624). When you get to step 3 make sure you are using the `node-inspector` command and ***not*** the `node-debug` command. The `node-debug` command both runs your script *and* starts up the node-inspector server. You just want to start the server, not start your script. Then you can start your script separately using the `--debug-brk` flag. – CatDadCode Mar 20 '15 at 15:00
22

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

Jerome Anthony
  • 7,823
  • 2
  • 40
  • 31
  • Very helpful! I too have had this stupid hanging problem when I'd rather just hit breakpoints right away. Thanks for the tip! – CatDadCode May 05 '16 at 18:54
6

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

lumio
  • 7,428
  • 4
  • 40
  • 56
elmpp
  • 550
  • 1
  • 7
  • 16
  • 1
    You just saved my day! No need for node-inspector anymore. – Steffen Langer Oct 18 '16 at 20:42
  • hmm, in my case it's giving me a URL starting with "ws://" ... the machine is a headless server, so I passed the host:port option, yet I still can't open the "ws://" URL in my local instance of Chrome – Michael Dec 08 '17 at 23:23
1

--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

vinay gosain
  • 171
  • 6
0

enter image description here

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.

clay
  • 5,917
  • 2
  • 23
  • 21
  • Thanks Clay, --debug-brk seem to have worked..it did't work initially though..but after trying few times..it did.. – genwip May 01 '14 at 14:15
0

node-debug --no-preload app.js

This what's working for me. According 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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Nevin Madhukar K
  • 3,031
  • 2
  • 25
  • 52