WitVault WitVault - 8 months ago 75
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

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


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

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