-->

How to debug a basic node.js application (not http

2019-01-30 03:47发布

问题:

I know how to debug http applications using node-inspector and iisnode. But can I use node-inspector to debug a non http node application, on windows?

I tried:

 node debug test.js

It says:

debugger listening on port 5858

But opening http://localhost:5858/ in Chrome does not do anything.


BTW: running node debug test.js does start the command-line debugger which works. But it's nothing like node-inspector.

回答1:

To use node-inspector, the right switch is node --debug not node debug

Here are the detailed steps:

  1. install node-inspector globally (npm install -g node-inspector)
  2. from a command-line window, run: node-inspector
  3. open Chrome and go to http://localhost:8080/debug?port=5858. You'll get the node-inspector UI but without any running app.
  4. from another command-line window, run your app with the --debug switch like this: node --debug test.js
  5. refresh the Chrome tab and voila!

A few interesting points:

  • If you kill your app and start it again, just refresh the node-inspector tab. It will keep all your breakpoints.
  • To break automatically on the first line start your app with node --debug-brk test.js


回答2:

Some links which might help you:

  • http://vimeo.com/19465332 (screencast from Ryan himself).
  • https://github.com/joyent/node/wiki/using-eclipse-as-node-applications-debugger


回答3:

It says: debugger listening on port 5858

I wondered myself about this but since the Node.js documentation indicates that the debugger is accessible via a simple TCP protocol and says nothing about HTTP my guess is that no, it won't be available at _http://localhost:5858.

"V8 comes with an extensive debugger which is accessible out-of-process via a simple TCP protocol" - http://nodejs.org/api/debugger.html



回答4:

Very recently Microsoft released the node.js tools for Visual Studio. It has the very comfortable Visual Studio debugging for node.js.



回答5:

node-inspector could be very helpful.

Use it from any browser supporting websockets.

Breakpoints, profiler, livecoding, etc..

http://erickrdch.com/2012/09/debug-a-nodejs-app-with-chrome-dev-tools.html



回答6:

FYI, in OSX 10.8, Chrome v26 doesn't seem to work, but Safari 6 does using the same instructions as above and using 0.0.0.0:8080 to conect.

There is another post by Danny Coates somewhere that says to do it in the following order:

  1. Your node process: node --debug (or --debug-brk) my_program.js
  2. Node-inspector: node-inspector
  3. The browser pointed to 0.0.0.0:8080


回答7:

If you are a noob like me on Windows, and you get 'node-inspector not recognized' or something about windows JScript error... despite global install, adding to PATH, etc. then this may help.

Navigate to C:\Users\urusername\AppData\Roaming\npm

Then run node-debug.cmd or node-inspector.cmd

You should get magical words like

Node Inspector v0.12.7
Visit http://127.0.0.1:8080/?port=5858 to start debugging.
Debugger listening on port 5858

Awesome. If you know of a better solution, please let me know