Skip to Content
User Experience Insights
Author's profile photo David Sooter

Debugging nodejs application in vscode running on SAP Cloud foundry

We always try to keep your dev environments as similar as possible to our production environments to avoid unwanted side effects when deploying our app. Unfortunately it’s not always possible and that often leads to ours of deploying and redeploying your app trying to figure out why your app just isn’t working as expected. If you haven’t logged enough info from your app finding the issue can be very time consuming and nerve-racking.

Wouldn’t it be nice to be able to debug your app in the cloud and be able to see step by step why its behaving the way it is?

 

The blog attempts to show how just that can be done

I was inspired to write this blog after reading a blog post written by Marius Obert CloudFoundryFun #7 the goal being to connect your local vscode to your application running on cloud foundry.

I wanted to take it a step further and debug it in vscode.

But enough talk lets get started.

We will create a sample app deploy it and debug it in our local vscode.

 

Our super simple express app offers a single endpoint. /debug

Ill include app the snippets used so you can follow along.

package.json

{
  "name": "debug-cloud",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "engines": {
    "node": "12.X"
},
  "scripts": {
    "start": "node --inspect index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1"
  }
}

 

One important note: your node version should be a version after v6.3.0 to be able to hande the node inspector on its own.

 

index.js

const express = require('express')
const app = express()


app.get('/debug', function(req, res){       
    res.send("Debug endpoint called")
 });
 
// Start server
app.listen(process.env.PORT || 8080, ()=>{})

 

Like i said our app only contains one endpoint but thats ok becase we are just trying to prove a point.

 

Ok we have our app set now lets deploy it. To do that define a manifest.yaml file

manifest.yaml

---
applications:
- name: debug-app
  memory: 128M
  random-route: true
  buildpacks:
    - nodejs_buildpack

 

After defining your manifest lets deploy our app

cf login #Perform you login to your cf account -- a demo account works as well

cf push #push your application

 

After your app has been pushed its time to enable ssh on your space and app

 

cf enable-ssh <app-name>
cf allow-space-ssh <space-name>
cf restage <app-name>​

After enabling ssh we have to restage the app for the changes to take effect if not you will get an unauthorized error

 

So now our work is done in the cloud lets get to debugging

Open vscode and add the following to your launch config.

 {
            "type": "node",
            "request": "attach",
            "name": "Attach cloud app ",
            "address": "localhost",
            "port": 9229,
            "localRoot": "${workspaceFolder}",
            "remoteRoot": "/home/vcap/app"
          }

 

After adding the run config its time to bind our local port to our cloud application

 

Open and running the following

cf ssh <APP_NAME> -N -T -L 9229:127.0.0.1:9229

 

What that does is it bind the port 9229 of your remote server to the local port 9229

Now all we have to de is run the debugger and test our breakpoint

To get the our apps endpoint all we have to do is run cf apps to get a list off all out apps and their endpoints.

So lets try it all out.

As you can see after connecting via ssh we are able to debug our app locally isn’t that nice.

This isn’t meant for every day use but more as a way to find those hard to find issues when deploying your app to Cloud Foundry.

Enjoy and Happy debugging.

Assigned Tags

      6 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Marius Obert
      Marius Obert

      Great post, David ?! I'm happy that I could inspire you.

      Author's profile photo Sandeep Malhotra
      Sandeep Malhotra

      Thanks David for a nice blog.

      I tried all the steps but failed to perform debugging . Below is the screenshot of error I got Connection%20Failed

      Connection Failed

      Please let me know the steps to troubleshoot this error.

      Regards,

      Sandeep

      Author's profile photo Sandeep Malhotra
      Sandeep Malhotra

      Thanks I managed to resolve the issue.  The real cause was I added inspect module explicitly in package.json which is not required with node version 12.x.x

      Author's profile photo Matheus Leao
      Matheus Leao

      I am getting this error and do not have Inspect module in package.json.

      How did you arrive at that solution?

      Any other solutions?

      Author's profile photo Sandeep Malhotra
      Sandeep Malhotra

      I am not using inspect module. I have only followed the steps mentioned in the blog. I have started using node v16.16.0  and have not faced any further issues.

      Do let us know what specific error you got

      Thanks

      Author's profile photo Clemens Fehr
      Clemens Fehr

      A common error that leads to these messages is to forget adding --inspect to scripts > start in package.json. Without it the node.js process will not listen for incoming debugging clients. See top of page for correct example.