How do I debug Node.js applications?

2018-12-31 07:36发布

How do I debug a Node.js server application?

Right now I'm mostly using alert debugging with print statements like this:

sys.puts(sys.inspect(someVariable));

There must be a better way to debug. I know that Google Chrome has a command-line debugger. Is this debugger available for Node.js as well?

30条回答
浪荡孟婆
3楼-- · 2018-12-31 08:18

IntelliJ works wonderfully for Node.js.

In addition, IntelliJ supports 'Code Assistance' well.

查看更多
听够珍惜
4楼-- · 2018-12-31 08:18

A quick-and-dirty way to debug small Node.js scripts with your favorite browser debugger would be to use browserify. Note that this approach doesn't work with any applications which require native I/O libraries, but it is good enough for most small scripts.

$ npm install -g browserify

Now move all your var x = requires('x') calls into a requires.js file and run:

$ browserify requires.js -s window -o bundle.js

(The downside here is that you either have to move or comment the requires in all your files.)

Include the bundle.js in an HTML file like so:

<script type="text/javascript" src="bundle.js"></script>

Now load the file in your browser and press F12 and viola: debug in browser.

查看更多
浮光初槿花落
5楼-- · 2018-12-31 08:20

Assuming you have node-inspector installed on your computer (if not, just type 'npm install -g node-inspector') you just have to run:

node-inspector & node --debug-brk scriptFileName.js

And paste the URI from the command line into a WebKit (Chrome / Safari) browser.

查看更多
怪性笑人.
6楼-- · 2018-12-31 08:20

Debugging

Profiling

  1. node --prof ./app.js
  2. node --prof-process ./the-generated-log-file

Heapdumps

Flamegraphs

Tracing

Logging

Libraries that output debugging information

Libraries that enhance stack trace information

Benchmarking

Other

Legacy

These use to work but are no longer maintained or no longer applicable to modern node versions.

查看更多
孤独总比滥情好
7楼-- · 2018-12-31 08:21

If you need a powerful logging library for Node.js, Tracer https://github.com/baryon/tracer is a better choice.

It outputs log messages with a timestamp, file name, method name, line number, path or call stack, support color console, and support database, file, stream transport easily. I am the author.

查看更多
登录 后发表回答