Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 438
JP Ventura Avatar asked Mar 10 '23 20:03

JP Ventura


2 Answers

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.

like image 37
Yoni Rabinovitch Avatar answered Mar 23 '23 10:03

Yoni Rabinovitch


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.

like image 97
Pablo Avatar answered Mar 23 '23 09:03

Pablo