简体   繁体   English

我可以使用VSCode在本地运行/调试Heroku Node.js应用程序吗?

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


TL; TL; DR DR

On Microsoft VSCode v1.6.1 , how to: Microsoft VSCode v1.6.1上 ,如何:

  1. Properly set runtime executable? 正确设置运行时可执行文件?
  2. Properly pass Heroku arguments ? 正确传递Heroku参数
  3. Run Heroku Node.js app? 运行Heroku Node.js应用程序?
  4. Debug Heroku Node.js app? 调试Heroku Node.js应用程序?

Details 细节

I have created a Heroku Node.js application, which is launched using the CLI command: 我创建了一个Heroku Node.js应用程序,它使用CLI命令启动:

heroku local web

and successfully starts at port 5000. 并成功从5000端口开始。

I am trying to debug it using Microsoft Visual Studio Code , using the following launch.json configuration: 我正在尝试使用Microsoft Visual Studio Code调试它,使用以下launch.json配置:

{
    "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: 但VSCode自动将--debug-brk参数传递给heroku,导致错误:

/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). VSCode也没有找到没有完整路径的heroku命令(似乎它没有加载PATH环境变量)。

Any ideas about how to setup the editor? 有关如何设置编辑器的任何想法?

The following solution works for me: 以下解决方案适合我:

1) In your procfile add the parameter --debug to the node process 1)在procfile中,将参数--debug添加到节点进程

web: node --debug server.js

By default the debugger listens in the port 5858 默认情况下,调试器侦听端口5858

2) Once you have the node process running, open VSCode and add the following configuration to your launch.json file 2)运行节点进程后,打开VSCode并将以下配置添加到launch.json文件中

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

3) Finally, click the play button in VSCode with the option "Attach to Process" and it should debug your process. 3)最后,单击VSCode中的播放按钮,选择“附加到进程”,它应该调试您的进程。

The following solution worked for me. 以下解决方案对我有用。 In my package.json "scripts", I added: 在我的package.json“脚本”中,我补充说:

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

Then, in launch.json I added an envFile entry to the default "Launch via NPM" configuration, which now looks looks like this: 然后,在launch.json中,我将一个envFile条目添加到默认的“通过NPM启动”配置,现在看起来像这样:

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

The above solution enables the VSCode debugger to run my server via an npm script, and my server runs with the env vars set in my .gitignore'd .env file, just like in the "regular" Heroku node.js workflow . 上述解决方案使VSCode调试器能够通过npm脚本运行我的服务器,并且我的服务器使用我的.gitignore'd .env文件中设置的env vars运行,就像在“常规” Heroku node.js工作流中一样

I struggled with this as for some reason the solution propsed didn't work for me. 由于某些原因,我提出的解决方案对我不起作用,因此我一直在努力。 However, an alternate solution did so I thought I would share. 但是,另一种解决方案确实如此,我想我会分享。

From the default debugging options in VS Code choose Attach by Process ID 从VS Code中的默认调试选项中选择Attach by Process ID

When you start the debugger with this configuration it should list available processes to attach to and one should be simply be server.js. 当您使用此配置启动调试器时,它应列出要附加到的可用进程,其中一个应该只是server.js。 This requires manually attaching each time, and if the other automatic attachment works for you that may be better, but this is still a workable solution. 这需要每次手动附加,如果其他自动附件适合您,可能会更好,但这仍然是一个可行的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM