NodeJS - a step-by-step debugger for NodeJS

前端 未结 7 519
太阳男子
太阳男子 2020-12-19 01:10

I\'m looking for effective way for step-by-step debugging NodeJS server code. At this moment I use dozens of console.log()\'s and it\'s extremely hard. The perfect tool is o

相关标签:
7条回答
  • 2020-12-19 01:45

    node inspect built-in CLI step debugging

    It took a while, but at some point Node finally added built-in debugging:

    node inspect main.js
    

    and this leaves you at the first line of the program.

    Navigate with:

    • n step to next line
    • s step into function
    • c continue
    • o step out
    • h get help

    In order to view variables or inject code, you need to first enter REPL mode with:

    repl
    

    and then you can run commands as in a node interactive session, e.g. to see the value of a variable just write the variable name:

    myvar
    

    It is awkward to have to type those four characters repl all the time, but I don't know any alternative.

    You can also add a debugger statement to where you want to break in the program after a c:

    const i = 1;
    debugger;
    i = 2;
    

    Related question: How do I debug Node.js applications?

    Tested in Node.js v10.15.1, Ubuntu 19.10.

    0 讨论(0)
  • 2020-12-19 01:46

    Even though question is very old, there is a awesome IDE from Microsoft "Visual Studio Code" which is developed in intention with Javascript (Typescript) which is capable of debugging Node JS projects

    Checkout this link https://code.visualstudio.com/docs/nodejs/nodejs-debugging

    0 讨论(0)
  • 2020-12-19 01:50

    Try nodeunit npm module for testing nodejs server side codes.

    0 讨论(0)
  • 2020-12-19 01:52

    Use node-inspector to provide the node debugging environment you're looking for. It's fantastic.

    0 讨论(0)
  • 2020-12-19 01:59

    How about this?

    You can try to test for Nodeclipse version 0.2.0 beta.

    http://www.tomotaro1065.com/nodeclipse/

    It will help you to debug node apps easily.

    GENERATING OF EXPRESS PROJECT

    Select the [File]-[New]-[Project] menu.
    Select [Node]-[Express Project], and push [Next] button.
    Enter [Project name], and push [Finish] button.
    

    DEBUGGING

    Open the JavaScript source files that you want to set breakpoints.
    Double-click on the ruler at the left end of the line you want to set a breakpoint.
    If you want to remove a breakpoint, double-click on the ruler again.
    Select the main source file of Node Application on the Project Explorer, 
    open the context menu by right-clicking, 
    select the [Debug As]-[Node Application] menu.
    
    0 讨论(0)
  • 2020-12-19 02:00

    Basically, Node.js is built on top of V8, so its debugging capabilities are also built on top of V8's debugging capabilities.

    V8 has an included debugger which can be accessed via TCP on port 5858.

    So basically all you need is a frontend which is able to connect to port 5858 and talk V8's debugging protocol.

    One option is to use node-inspector which basically provides a debugging UI in your browser. Unfortunately, it does only work with Google Chrome and Apple Safari (which for me is no problem, but there may be others ;-)).

    Another option is to use a plugin for Eclipse.

    And, last but not least, the built-in debugger of Node.js (which always reminds me of MS-DOS's edlin) is also just a front-end for this TCP debugger, just a built-in one.

    And of course, there are much more options ... these three were just the first three ones that came to my mind ;-)

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