Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug node js app with breakpoints and everything?

I've installed node-inspector just to find out that it doesn't support breakpoints :| What's the point in it at all, bearing in mind that on big part node code is asynchronous and you simply cannot follow it step by step?..

I'm definitely missing a point here...

Anyway to debug node code with breakpoints and everything?

like image 411
jayarjo Avatar asked Jul 23 '12 10:07

jayarjo


People also ask

How do you set a breakpoint in node JS?

When debugging in Node. js, we add a breakpoint by adding the debugger keyword directly to our code. We can then go from one breakpoint to the next by pressing c in the debugger console instead of n . At each breakpoint, we can set up watchers for expressions of interest.

Which of the following tools is used to debug Node JS application?

js Debugger. Node. js provides built-in non-graphic debugging tool that can be used on all platforms. It provides different commands for debugging Node.


2 Answers

yupp, I've successfully used node-inspector. If you want permanent breakpoints, simply insert debugger; in your code. See http://nodejs.org/api/debugger.html.

Making node wait until a debugger is attached, using node --inspect-brk script.js (previously node --debug-brk script.js), can also be very helpful.

like image 155
rdrey Avatar answered Sep 22 '22 06:09

rdrey


(For Node 8 and later)

Node.js has a built-in debugger. Normally you can turn on the debugger in two ways:

  1. Start your Node.js app or script with the --inspect or --inspect-brk switch. For example:

    $ node.js --inspect index.js

(Note: --inspect-brk breaks before user code starts)

  1. If for some reason you cannot start your Node.js app or script with the --inspect switch, you can still instruct the Node.js process to start listening for debugging messages by signalling it with SIGUSR1 (on Linux and OS X). For Node 8 and later it will activate the Inspector API, same as the --inspect switch

    $ kill -sigusr1 23485

(Note: you need to replace 23485 with your own Node.js process ID)

With the debugger turned on, you can open the Google Chrome browser, and type in the address bar chrome://inspect

Then you should see an entry listed under "Remote Target". Go ahead and click "inspect".

Now you can set breakpoints and start debugging your code.

Reference:

  1. https://nodejs.org/en/docs/guides/debugging-getting-started/
  2. Related issue on stackoverflow: Chrome Devtools Dedicated Node.js Inspector not stopping at breakpoints
like image 34
Yuci Avatar answered Sep 22 '22 06:09

Yuci