Can I run/debug Heroku Node.js app locally with VSCode?

时间:2016-10-20 19:40:56

标签: node.js heroku visual-studio-code


TL; DR

On Microsoft VSCode v1.6.1, how to:

  1. Properly set runtime executable?
  2. Properly pass Heroku arguments?
  3. Run Heroku Node.js app?
  4. Debug Heroku Node.js app?

Details

I have created a Heroku Node.js application, which is launched using the CLI command:

heroku local web

and successfully starts at port 5000.

I am trying to debug it using Microsoft Visual Studio Code, using the following launch.json configuration:

{
    "name": "Launch",
    "type": "node",
    "request": "launch",
    "program": "${workspaceRoot}/app.js",
    "stopOnEntry": false,
    "args": [],
    "cwd": "${workspaceRoot}",
    "preLaunchTask": null,
    "runtimeExecutable": "/usr/local/bin/heroku",
    "runtimeArgs": [
        "local web",
    ],
    "env": {
        "NODE_ENV": "development"
    },
    "console": "internalConsole",
    "sourceMaps": false,
    "outFiles": []
}

But VSCode is automagically passing --debug-brk argument to heroku, causing the error:

/usr/local/bin/heroku --debug-brk=23080 'local web' app.js 
    !    `--debug-brk=23080` is not a heroku command.
    !    See `heroku help` for a list of available commands.

VSCode also does not find heroku command without its full path (seems like it is not loading PATH environment variable).

Any ideas about how to setup the editor?

3 个答案:

答案 0 :(得分:3)

以下解决方案适合我:

1)在procfile中,将参数--debug添加到节点进程

web: node --debug server.js

默认情况下,调试器侦听端口5858

2)运行节点进程后,打开VSCode并将以下配置添加到launch.json文件

{
 "type": "node",
 "request": "attach",
 "name": "Attach to Process",
 "port": 5858
}

3)最后,单击VSCode中的播放按钮,选择" Attach to Process"它应该调试你的过程。

答案 1 :(得分:1)

由于某种原因,我提出的解决办法对我不起作用,因此我很挣扎。但是,另一种解决方案确实如此,我想我会分享。

从VS Code中的默认调试选项中选择 Attach by Process ID

使用此配置启动调试器时,应列出要附加的可用进程,其中一个应该只是server.js。这需要每次手动附加,如果其他自动附件适合您,可能会更好,但这仍然是一个可行的解决方案。

答案 2 :(得分:1)

以下解决方案适合我。 在我的package.json“脚本”中,我添加了:

 "debug": "node --inspect-brk server.js"

然后,在launch.json中,我在默认的“通过NPM启动”配置中添加了envFile条目,现在看起来像这样:

 {
        "type": "node",
        "request": "launch",
        "name": "Launch via NPM",
        "runtimeExecutable": "npm",
        "runtimeArgs": [
            "run-script",
            "debug"
        ],
        "port": 9229,
        "envFile": "${workspaceFolder}/.env"
}

上述解决方案使VSCode调试器能够通过npm脚本运行我的服务器,并且我的服务器使用我的.gitignore'd .env文件中设置的env vars运行,就像在“常规”Heroku node.js workflow中一样。