使用VisualStudioCode远程调试NodeJS程序

Kagula

2018-08-02

环境:
[1]NodeJS v8.9.1
[2]Visual Studio Code 1.25.1
[3]有两台计算机:
   第一台计算机上装了linux系统, 运行nodeJS程序.
   我们称之为远程计算机,  ip地址为192.168.168.168.
   第二台计算机上装了windows系统, 运行Visual Studio Code, 要远程调试第一台计算机上的NodeJS程序.
   我们称之为本地计算机.

第一步:在远程计算机上启动远程Listen
node --inspect=XX:9229  xxx.js
其中XX是ip地址,  不能用127.0.0.1必须要用192.168.168.168这种形式的,  否则其它计算机会远程不到你运行NodeJS的计算机.
如果你的Node.JS是多进程程序, 会打印出类似下面的信息
[root@localhost bimServer]# ./run_app test
Debugger listening on ws://192.168.168.168:9229/0f2f6b22-3deb-4a23-b148-e5bfbcf56bad
For help see https://nodejs.org/en/docs/inspector
Debugger listening on ws://192.168.168.168:9230/3aa39861-0d96-404e-a075-62bbcf825c4e
For help see https://nodejs.org/en/docs/inspector
...
第一个进程在9229端口listen, 第二个端口在9230端口listen.


第二步:在本地计算机上查看远程调试功能是否已经启动了.
http://192.168.168.168:9229/json/version
如果远程调试功能正确启动的话, 会打印出下面的信息
[ {
  "description": "node.js instance",
  "devtoolsFrontendUrl": "chrome-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=192.168.168.168:9229/0f2f6b22-3deb-4a23-b148-e5bfbcf56bad",
  "faviconUrl": "https://nodejs.org/static/favicon.ico",
  "id": "0f2f6b22-3deb-4a23-b148-e5bfbcf56bad",
  "title": "3drepo.js",
  "type": "node",
  "url": "file:///Zdata/wwwroot/bimServer/3drepo.js",
  "webSocketDebuggerUrl": "ws://192.168.168.168:9229/0f2f6b22-3deb-4a23-b148-e5bfbcf56bad"
} ]


第三步:
Visual Studio Code中启动远程调试launch.json清单
注意这里用了9230端口,而不是9229端口,是因为我们的Node程序比较特殊,会启动两个进程,
这里我是要跟踪NodeJS的第二个进程,
第一个进程在9229端口侦听,  第二个进程在9230端口侦听.
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [        
        {
            "type": "node",
            "request": "attach",
            "name": "远程调试",
            "address": "192.168.168.168",
            "port": 9230,
            "localRoot": "${workspaceFolder}",
            "remoteRoot": "/Zdata/wwwroot/bimServer"
        },
        {
            "type": "node",
            "request": "launch",
            "name": "启动程序_1_12_9",
            "program": "${workspaceFolder}/3drepo.js",
            "cwd": "${workspaceRoot}",
            "env":{"NODE_ENV":"test","NODE_CONFIG_DIR":"config"},
            "autoAttachChildProcesses": true
        }
    ]
}


Reference

[1]

https://nodejs.org/en/docs/guides/debugging-getting-started/

[2]

调试chrome中前端代码的Launch.json参考

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Attach to Chrome",
            "type": "chrome",
            "request": "attach",
            "port": 9223,
            "sourceMaps": true,
            "url": "http://bim.v/Project/Progress/plan",
            "webRoot": "${workspaceFolder}/public"
        }
    ]
}

 

你可能感兴趣的:(JavaScript或前端,NodeJS)