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
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
Share
Improve this question
asked Nov 11, 2016 at 15:14
WitVaultWitVault
24.1k20 gold badges106 silver badges137 bronze badges
3
- 1 Node 4 didn't have this experimental feature. It appeared in node 6. Probably you were reading latest docs. This one is for node 4: nodejs/dist/latest-v4.x/docs/api/debugger.html – Andrey Commented Nov 11, 2016 at 15:30
- If you're running node.js v4 then node-inspector is worth a look – dan Commented Nov 11, 2016 at 15:38
- A related post here related to development/debugging in NodeJs. – RBT Commented Jan 25, 2018 at 6:38
3 Answers
Reset to default 8You need node 6.3 or above --inspect is not supported in 4.4.7
@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.
Node Inspector works pretty well. Also keep in mind Node 7 has native node debugging, even supports live-edit! Here's a gif of it in action: Chrome DevTools: Live edit running Node.js code with hotswapping