NodeJS - Debugging


If we prefer not to use console.log, or we think that our problem can be better 
analyzed using breakpoints, we can use the node's built-in debugger.

We will need to use a Blink-based browser such as Chrome to debug.  

node-inspector provides the node-debug command which is a drop-in replacement 
for node.  We can run it:

node-debug fileName.js

or we can use:

node debug fileName.js

This will launch the specified script in Node with debugging enabled and open 
the browser UI (in your default browser).  If Chrome is not our default browser, 
we will have to copy and paste this URL into the Chrome browser's address bar 
and hit the Enter key.  At this point we should see Chrome’s debugging UI. We 
can press F8 to continue script execution until your debugger statement is 
reached. From this point we can debug as usual. In this environment, we only 
have access to inspect and debug the test module, not the AUT.  This UI is 
provided by node-inspector.  The Chrome's developer tool is not actually open.

I was in a Node environment that has a separate debug library. To launch my node service in debug mode, I need to run 'npm run debug', which is equivalent to 'node debug dist/server.js'. This will print a message to the terminal. I will have to press the letter c and hit the enter key. To debug my code, I have to add the debugger keyword to my code, and re-run the 'npm run debug' command, and at the terminal, I have to type 'repl' and hit the enter key. This will put me into the debug mode. I can then make HTTP request to my service, and it will pause at the line where I have added the 'debugger' statement. I can then do console.dir to dump out the value of my variable.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License