Node.js: How to attach to a running process and to debug the server with a console?

后端 未结 7 1441
無奈伤痛
無奈伤痛 2020-12-04 21:51

I use \'forever\' to run my application. I want to attach to the running environment to inspect my application. So what can I do?

7条回答
  •  醉话见心
    2020-12-04 22:25

    Starting from Node 6.3, node has a built-in debugger that can be triggered (even in a production app) by doing:

    kill -USR1 
    

    The node process will spit out something like this:

    Debugger listening on ws://127.0.0.1:9229/f3f6f226-7dbc-4009-95fa-d516ba132fbd
    For help see https://nodejs.org/en/docs/inspector
    
    • If you can access the server from a browser, you can use chrome://inspect on http://host.domain:9229.
    • If you cannot connect via a browser (e.g. the server is in a firewalled production cluster), you can activate a REPL to inspect over the command line:

      node inspect -p 
      
    • If you can't access the server from a browser, but you can SSH into that server, then setup SSH port forwarding (ssh -nNTL 9229:localhost:9229 @ -i .pem) and you'll find your script under chrome://inspect after a few seconds.

    Prior to this version, node-inspector was a separate tool for debugging Node processes. However, as documented on its own page, it is mostly deprecated as the now-bundled debugger is actively maintained and provides more advanced features. For more information on this change, see this thread.

提交回复
热议问题