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

后端 未结 7 1428
無奈伤痛
無奈伤痛 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:28

    Windows users

    If you are on Windows that doesn't support POSIX signals, you can use this workaround from another cmd.

    node -e "process._debugProcess(PID)"
    




    For a detailed guide, or to set up debugging in VSCode, follow these simple steps:

    1. In VSCode, open launch.json configuration or create new by clicking on the wheel
      (this is the debug view CtrlShiftD)

    1. The node will listen on port 9229 by default, so add this configuration:
    {
      "type": "node",
      "request": "attach",
      "name": "Attach to 9229",
      "port": 9229
    },
    
    1. Open Task Manager and locate the PID of your node process
      I could identify my by the "build" folder where the index.js is.
    2. open another cmd or git-bash and run this command,
      where 21392 is the PID of your process.
    node -e "process._debugProcess(21392)"
    
    1. you should see this
    2. Start debugging from VSCode Attach to 9229

    Everything should be ready now.

提交回复
热议问题