WitVault WitVault - 26 days ago 19
Node.js Question

Debugging local nodejs script in chrome dev tools

I have a stand alone node js script.
I am doing some local task with that script. I want to debug that script in chrome dev tools. I know I can debug it locally via putting debugger in code but do not want to do that.

On nodejs docs I saw that it has some options like -


V8 Inspector Integration for Node.js# NOTE: This is an experimental
feature.

V8 Inspector integration allows attaching Chrome DevTools to Node.js
instances for debugging and profiling.

V8 Inspector can be enabled by passing the --inspect flag when
starting a Node.js application. It is also possible to supply a custom
port with that flag, e.g. --inspect=9222 will accept DevTools
connections on port 9222.

To break on the first line of the application code, provide the
--debug-brk flag in addition to --inspect.

$ node --inspect index.js


But when I do that it gives me error like -

$ node --inspect index.js
node: bad option: --inspect


My node version is:

$ node --version
v4.4.7

Answer

@Andrey Thanks a lot for pointing out that I was scratching my head for a long time on this. @dan Thanks for node-inspector suggestion.

I am just posting an answer in case someone else might stuck on this.

I have installed node-inspector and,

This is what I'm doing to start the debugger.. Opened one terminal and

$ node-inspector --no-preload
Node Inspector v0.12.5
Visit http://127.0.0.1:8080/?ws=127.0.0.1:8080&port=5858 to start debugging.

In another terminal,

$ node --debug-brk app.js 
debugger listening on port 5858

Intially I was using just --debug. But it was not hitting the breakpoints and was going through all code. Then I used --debug-brk. Using --debug-brk caused node to break on the first line of app and waited for a debugger to hit breakpoints.

Then started Google Chrome and went to http://127.0.0.1:8080/debug?port=5858

Here chrome dev tools was opened and I was able to put break point and debug the code.