An easy way to debug Node.JS apps in Cloud Foundry
There are some blogs on how to debug a Node.JS application in CF, but it’s related to the old Node.JS versions, where you should add “node-inspector” and play with the path to the node executable file to invoke the node-inspector.
From the latest node version (v6.3.0+) it’s not needed anymore. To help colleagues that are struggling making it work, I decided to write this short blog explaining how to do it…
Following is step by step guide, how to get it up and debugging
Don’t try it at home/prod :-)
- You should change the start script in the package.json to run with the
inspect flag like following: (app.js is your application starting point)
“start”: “node --inspect app.js”
2. Add node engine entry to your package.json (this step tell to the node.js build-pack which node version should be installed in the application container)
“engines”: {
“node”: “7.10.0”
}
3. Push the application:
Note : Since not all the platforms Build-pack is supporting the latest node engines you should use the following command, which run the latest node build-pack from Github.
Use:
cf push -b https://github.com/cloudfoundry/nodejs-buildpack
Note: We use version “7.10.0” since this is the minimum node version that supported with the latest (The build-pack supported engine version may be changed frequently, you can see the exact version here) Node.JS build-pack that published on GitHub. (if your platform/custom build-pack support it you just need to simply push your application without the -b flag and the reference URL)
For more info see: https://github.com/cloudfoundry/nodejs-buildpack/releases
4. Create SSH Tunnel via the terminal that you can remotely access the application debug port which by default is 9229
cf ssh -N -T -L 9229:127.0.0.1:9229 <applicationName>
Note: To enable SSH access to your app, SSH access must also be enabled for both the space that contains the app and the application. you can do it like following:
cf enable-ssh <applicationName>
And
cf allow-space-ssh SPACE_NAME
5. Now you should get the URL of the Chrome dev-tools from the application log
cf logs <applicationName> --recent
6. Put The URL (can take a few seconds to connect) in the browser and click on Sources, open the tree file entry and navigate to the application starting file app.js/index.js/server.js, etc) and put a break-point(see the screen-shot below).
7. Run the application URL in the browser and it should stop at the break-point you set.
It should look like:
Enjoy!
