There may be better methods out there, but the one I use works well for me. Here's how I do it:
To enable debugging, you'll need to modify the start command
in your app's manifest.yml file. For instance:
command: node --inspect --require ts-node/register src/index.ts
If you're working with JavaScript instead of TypeScript, the command would look something like this:
command: node --inspect src/index.js
In order to connect your debugger to the inspector, you have to set up an ssh tunnel to your app using the following command:
cf ssh <APP_NAME> -N -T -L 9229:127.0.0.1:9229
This command will link port 9229 on your local machine to port 9229 on the container where your app is running (9229 being the default inspector port).
Lastly, assuming you are using VS code, you'll need to configure your debugger. Here's the setup I typically use:
{
"type": "node",
"request": "attach",
"name": "Attach to Remote",
"address": "localhost",
"port": 9229,
"localRoot": "${workspaceFolder}",
"remoteRoot": "/home/vcap/app"
}
The drawback of this method (compared to debugging a Java application) is that you cannot attach to a running app without starting it with --inspect
, which may not be ideal for production environments. Unfortunately, I haven't yet found a workaround for this. Consider deploying a separate version if you're already live.