Egg VSCode 调试
发布于 6 年前 作者 i5ting 8439 次浏览 来自 分享

正好看到https://cnodejs.org/topic/5ad6dc87464b1bfa6b425573,补充一点vscode下的调试

在项目中npm run debug其实是egg-bin debug。而egg-bin debug的核心在于inspector-proxy,也就是说egg-bin debug会启动调试进程。

为了让项目已进入就里面进入断点,有增加了-- 和 --inspect-brk 参数,这样就可以将–inspect-brk传入node inspect。

如果大家真的了解原理,其实调试也是非常简单的。首先npm run debug启动调试进程,然后再vscode里以attach附加到进程的方式就可以了。

你需要在launch.json里配置如下。

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Egg Debug",
            "runtimeExecutable": "npm",
            "runtimeArgs": [
                "run",
                "debug",
                "--",
                "--inspect-brk"
            ],
            "console": "integratedTerminal",
            "restart": true,
            "protocol": "auto",
            "port": 9229,
            "autoAttachChildProcesses": true
        },
        {
            "type": "node",
            "request": "launch",
            "name": "启动程序",
            "program": "${workspaceFolder}/--title=egg-server-example"
        }
    ]
}

在调试模式,选择Egg Debug,然后开始调试即可。

3 回复

而 egg-bin debug 的核心在于 inspector-proxy

这个结论不是很对:

  • inspector-proxy 是早期用来做调试端口代理用的,解决 worker 重启后的固定端口问题,后面 VSCode 已经内置支持了,这个库就结束历史使命了。
  • 可以看下源码 https://github.com/eggjs/egg-bin/blob/generator/lib/cmd/debug.js
  • 其实现在 egg-bin debug 等价于 EGG_DEBUG=true egg-bin dev --inspect-brk ,只是给 Node 传递 --inspect 这个 execArgv 而已

最新版的 VSCode,这些 launch 配置也可省掉了,直接打开 auto attach,然后直接 npm run debug 就可以了。(当然,配置 launch 有 F5 更好)

好久没见狼叔发帖了,帮你顶顶

回到顶部