简体   繁体   中英

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


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?

The following solution works for me:

1) In your procfile add the parameter --debug to the node process

web: node --debug server.js

By default the debugger listens in the port 5858

2) Once you have the node process running, open VSCode and add the following configuration to your launch.json file

{
 "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.

The following solution worked for me. In my package.json "scripts", I added:

 "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:

 {
        "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 .

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

When you start the debugger with this configuration it should list available processes to attach to and one should be simply be 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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