How to debug “built” production NodeJS

不问归期 提交于 2021-01-01 09:25:02

问题


I am using Visual Studio code to debug a node application in production environment

The Node process runs inside docker,

I port-forwarded and signaled USR1 to enable attaching debugger from VS code to that node process

My VS Code configuration is like this

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "attach",
      "name": "Debug: service",
      "sourceMaps": true,
      "smartStep": true,
      "remoteRoot": "/src/",
      "localRoot": "/home/my-username/work/orders/src/",
      "protocol": "inspector",
      "port": 9229,
      "restart": true,
      "address": "0.0.0.0",
      "skipFiles": [
        "<node_internals>/**",
        "<node_modules>/**"
      ]
    }
  ]
}

From VS code, I can hook into the application and the application can break on exception However there is no source-mapping which cause all my breakpoint in my source-code to be "unbound breakpoint"

The loaded script list in VS code show that

The VS code debugger is able to see the node_modules and the built version of my source code inside dist. One other notable point is that the source code that is used to build the /dist is also available directly in the production server, in the upper folder.

How can I debug the built production process, using my unbuilt source code in this case?

I added Chrome behaviour as separate question

NodeJs: Chrome inspector can map source but unable to debug on original source


回答1:


I don't know whether it will be helpful to you or not. But I think you have to use node-inspector. It can be used from any browser supporting WebSocket. It is really good.

Cool stuff

  • Node Inspector uses WebSockets, so no polling for breaks.
  • Remote debugging and debugging remote machine.
  • Live edit of running code, optionally persisting changes back to the file-system.
  • Set breakpoints in files that are not loaded into V8 yet - useful for debugging module loading/initialization.
  • Embeddable in other applications - see Embedding HOWTO for more details.



回答2:


Node already ships with an integrated debugger. However, it doesn’t have a GUI, so you need to use the command line version.

You can launch this debugger using node debug.

$node debug test.js
< Debugger listening on port 5858
debug> . ok
break in test.js:1
> 1 var a= 5;
  2 a = a*a
  3 a += 2;
debug>

It shows you where it’s paused and then lets you control execution with commands like next and cont.

debug> next
break in test.js:2
  1 var a= 5;
> 2 a = a*a
  3 a += 2;
  4

The repl and watch commands allow you to see the values of local variables.



来源:https://stackoverflow.com/questions/65119801/how-to-debug-built-production-nodejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!