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.
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.
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.
You can view logs with the Heroku CLI, the dashboard, your logging add-on, or in your log drain.
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.
2019 Answer for Webstorm 2019.2
Not much changed but still:
Create a file name Procfile
(no extension) --> Edit it:
web: node --inspect=9090 ./server.js
(or ./app.js or whatever your entry point is)
Deploy the code with the new Procfile!
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]
Now you are suppose to be attached to your heroku server.
In the end: Remove the Procfile and deploy again without it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With