Running nodejs 0.8.x (or any other version) on OpenShift

This week I had a quick look at Red Hat’s OpenShift platform. After a very good first impression I decided to create a sort of “hello world” node app to see the gears in motion. So I look around…. cartdriges… and voila! There it is, nodejs… but its 0.6… hmmm… scroll down a bit more… DIY [experimental]… aha! Next thought: Why don’t I use the DIY thing to try and install node 0.8.x, or even better, something that allows me to chose which nodejs version I want to run.

To my surprise, two coffees later there it was, working fine and doing what I was expecting it to do. Even if this is a half-baked solution (to say the least), it may still help someone out there trying to run nodejs v0.8.x on OpenShift. So why not share it. Before I wrote up this post I found a post on the OpenShift community explaining how to run 0.8 on OpenShift, but I thought this could be simplified a bit using nave, a script by Isaac Z. Schlueter based on Tim Caswell’s “nvm” and Kris Kowal’s “sea” programs.

To start with I created a new app using the DIY cartridge and cloned the git repo on my computer. Looking at the files included by default in the repo there was a directory named .openshift which contained action_hooks and cron. In this article I won’t go into the cron stuff, thats a completely different topic. But the action_hooks look like they would give us the tools we need, together with the incredibly helpful nave.

After messing about for a bit my file structure looked a bit like this:

??? .gitignore
??? .openshift
??? ??? action_hooks
??? ??? ??? build
??? ??? ??? deploy
??? ??? ??? post_deploy
??? ??? ??? pre_build
??? ??? ??? start
??? ??? ??? stop
??? ??? cron (omitting dir contents)
??? app.js
??? package.json
??? public
??? ??? favicon.ico
??? ??? style.css
??? views
    ??? index.hbs
    ??? layout.hbs

I added a package.json file with the normal stuff including dependencies. Im using handlebars to render views and serving static content from public/. I also added app.js which contains the application code. Now we need to make sure the hooks get to install and choose the right version of nodejs and then start the server using this version of nodejs. To do this we concentrate on 4 files:

  • .openshift/action_hooks/pre_build
  • .openshift/action_hooks/start
  • .openshift/action_hooks/stop
  • app.js

The gists below should speak for themselves.





With all of this in place, when I commit my changes and push them to the server the pre_build script is doing its job and installing all my dependencies. The app also starts and stops normally and all of it running nodejs v0.8.10. Nice.

Bear in mind that this example is very rough and nowhere near perfect, but it shows some of the possibilities and how easy it is to put the different parts together. For the time being the code in this example is running here: