How do I debug Node.js applications?

后端 未结 30 2073
暗喜
暗喜 2020-11-22 05:56

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.inspe         


        
相关标签:
30条回答
  • 2020-11-22 06:21

    I created a neat little tool called pry.js that can help you out.

    Put a simple statement somewhere in your code, run your script normally and node will halt the current thread giving you access to all your variables and functions. View/edit/delete them at will!

    var pry = require('pryjs')
    
    class FizzBuzz
    
      run: ->
        for i in [1..100]
          output = ''
          eval(pry.it) // magic
          output += "Fizz" if i % 3 is 0
          output += "Buzz" if i % 5 is 0
          console.log output || i
    
      bar: ->
        10
    
    fizz = new FizzBuzz()
    fizz.run()
    
    0 讨论(0)
  • 2020-11-22 06:21

    The NetBeans IDE has had Node.js support since version 8.1:

    <...>

    New Feature Highlights

    Node.js Application Development

    • New Node.js project wizard
    • New Node.js Express wizard
    • Enhanced JavaScript Editor
    • New support for running Node.js applications
    • New support for debugging Node.js applications.

    <...>

    Additional references:

    1. NetBeans Wiki / NewAndNoteworthyNB81.
    2. Node.js Express App in NetBeans IDE, Geertjan-Oracle.
    0 讨论(0)
  • 2020-11-22 06:23

    A lot of great answers here, but I'd like to add my view (based on how my approach evolved)

    Debug Logs

    Let's face it, we all love a good console.log('Uh oh, if you reached here, you better run.') and sometimes that works great, so if you're reticent to move too far away from it at least add some bling to your logs with Visionmedia's debug.

    Interactive Debugging

    As handy as console logging can be, to debug professionally you need to roll up your sleeves and get stuck in. Set breakpoints, step through your code, inspect scopes and variables to see what's causing that weird behaviour. As others have mentioned, node-inspector really is the bees-knees. It does everything you can do with the built-in debugger, but using that familiar Chrome DevTools interface. If, like me, you use Webstorm, then here is a handy guide to debugging from there.

    Stack Traces

    By default, we can't trace a series of operations across different cycles of the event loop (ticks). To get around this have a look at longjohn (but not in production!).

    Memory Leaks

    With Node.js we can have a server process expected to stay up for considerable time. What do you do if you think it has sprung some nasty leaks? Use heapdump and Chrome DevTools to compare some snapshots and see what's changing.


    For some useful articles, check out

    • RisingStack - Debugging Node.js Applications
    • Excellent article by David Mark Clements of nearForm

    If you feel like watching a video(s) then

    • Netflix JS Talks - Debugging Node.js in Production
    • Interesting video from the tracing working group on tracing and debugging node.js
    • Really informative 15-minute video on node-inspector

    Whatever path you choose, just be sure you understand how you are debugging

    It is a painful thing
    To look at your own trouble and know
    That you yourself and no one else has made it

    Sophocles, Ajax

    0 讨论(0)
  • 2020-11-22 06:23

    Visual Studio Code has really nice Node.js debugging support. It is free, open source and cross-platform and runs on Linux, OS X and Windows.

    You can even debug grunt and gulp tasks, should you need to...

    0 讨论(0)
  • 2020-11-22 06:24

    I put together a short Node.js debugging primer on using the node-inspector for those who aren't sure where to get started.

    0 讨论(0)
  • 2020-11-22 06:24

    Start your node process with --inspect flag.

    node --inspect index.js

    and then Open chrome://inspect in chrome. Click the "Open dedicated DevTools for Node" link or install this chrome extension for easily opening chrome DevTools.

    For more info refer to this link

    0 讨论(0)
提交回复
热议问题