NodeJS - a step-by-step debugger for NodeJS

前端 未结 7 523
太阳男子
太阳男子 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.

提交回复
热议问题