Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How could I debug a node.js App deploy on Heroku?

Tags:

node.js

heroku

Hi is there a way to debug an node.js app deployed on heroku server.

I cannot debug it locally since the app is receiving some post request from a 3rd party, and I don't have a public IP.

Thanks.

like image 581
RaresI Avatar asked Jul 25 '16 13:07

RaresI


People also ask

How do I debug an app on Heroku?

If running heroku local does not help, run a remote shell: heroku run -a <your app name> /bin/bash , and there run npm start to see the logs and debug the issue on the Heroku server.

Can you debug Node js?

There are a few ways you can debug your Node. js programs in VS Code: Use auto attach to debug processes you run in VS Code's integrated terminal. Use the JavaScript debug terminal, similar to using the integrated terminal.

How do I check Heroku console logs?

You can view logs with the Heroku CLI, the dashboard, your logging add-on, or in your log drain.


2 Answers

I don't know why Heroku doesn't highlight this better, but the documentation you need for remote debugging with Heroku is on this Heroku Exec (SSH tunneling) page. Currently it's node.js information needs a bit of an update to support the newer inspector protocol, but the important details are all there.

First you need to edit the Procfile at the root of your project tree to tell Heroku to start Node with debugging enabled:

web: node --inspect ./server.js (or ./app.js or whatever your entry point is)

Then you need to use the Heroku CLI utility to tunnel the debugging port between your computer and your Heroku application. If you don't have that installed already, read the docs on how to install it and log it into your Heroku account.

To enable the connection you'll need to run this once:

heroku ps:exec

or if you run multiple Heroku apps:

heroku ps:exec -a my-app-name

Once you get a $ command prompt, hit ctrl-C to exit.

Next, start the tunnel:

heroku ps:forward 9229 [-a my-app-name]

Now your application is available from any IDE that supports Node.js debugging. For Visual Studio Code, you'll want to create a launch.json configuration entry that looks like this:

{
    "type": "node",
    "request": "attach",
    "name": "Heroku",
    "address": "localhost",
    "port": 9229,
    "protocol": "inspector",
    "localRoot": "${workspaceFolder}",
    "remoteRoot": "/app"
},

You should now be able to start the debugging session in your editor and have it connect to your running app within a few seconds. All the normal debugging features, including breakpoints, call stacks, variable inspection etc should work.

like image 73
Bruce Avatar answered Sep 28 '22 02:09

Bruce


2019 Answer for Webstorm 2019.2
Not much changed but still:

  1. Create a file name Procfile (no extension) --> Edit it:

     web: node --inspect=9090 ./server.js 
    

    (or ./app.js or whatever your entry point is)

  2. Deploy the code with the new Procfile!

  3. Enable a connection - Open Terminal and run:

     heroku ps:exec -a [YOUR_APP_NAME]
    

Once you get a $ command prompt write:

    exit

in Terminal:

    heroku ps:forward 9090 -a [YOUR_APP_NAME]
  1. Create a "Run Configuration":
  • "Edit Configurations" --> Click on the + --> "Attach to node/Chrome" --> change port to 9090 & name it. enter image description here
  • Select "Chrome or Node > 6.3 started with --inspect"
  • Ok
  • Click the debug

Now you are suppose to be attached to your heroku server.

In the end: Remove the Procfile and deploy again without it.

like image 45
chenop Avatar answered Sep 28 '22 04:09

chenop