May 28, 2011 ( ruby, coffeescript )

Setting up CoffeeScript in Rails 3

About a year ago I attempted to start a blog, mainly to write about Ruby, basic server provisioning and other related technologies to share my knowledge and experiences to help other people out or start discussions. Unfortunately for various reasons I wasn’t able to spend enough time to do this back then.

Thankfully this is no longer the case and I’m excited to pick it up again!

Last week I decided to throw away the few articles I wrote up back then since they are pretty much obsolete now. I re-designed and rewrote my blog. Hope you like it!

Now, on to the actual article

As of late, more and more people are getting in to CoffeeScript. It was previously mentioned that CoffeeScript would ship with Ruby on Rails 3.1. Although you can easily switch it off if you prefer to write raw JavaScript. But it’s nice that they provide the option to quickly get up and running with CoffeeScript if you wanted to, I know I’ll be using it!

I recently released a new application I was working on and I had trouble getting CoffeeScript working in my production environment. CoffeeScript requires the V8 engine. I was using Node which enabled me to use CoffeeScript by installing NPM and then installing CoffeeScript via NPM. This was tragic. I previously somehow got it working in my staging environment, but when I tried to push it to production I simply could not get it to compile my .coffee files to .js files. I figured it had something to do with the load paths even though I set them up. I asked a bunch of people for help regarding this, but unfortunately no one I asked had a direct answer to my Node/NPM problem.

The solution

After spending a good amount of hours researching this, someone pointed out there is an open source project called The Ruby Racer. This is the V8 runtime, embedded in Ruby - sound cool? Well, it is. Since I only installed Node and NPM for the purpose of being able to use CoffeeScript, I figured that it’s pretty much overkill to use it.

I removed Node and read up on The Ruby Racer. It turned out to be incredibly easy to set up!

In a typical Ruby on Rails 3 app that ships with Bundler, all you define are the following gems in your Gemfile:

gem 'therubyracer'
gem 'barista'

That’s it. No Node, no NPM, just 2 gems defined in the Gemfile.

Barista is a gem that seamlessly integrates CoffeeScript support in to your Rails or other Rack-based applications. It only took me a minute to set up.

So if you want to use CoffeeScript, and nothing else Node related, you should definitely take a look at this solution before installing Node just for the purpose of being able to use V8 with CoffeeScript in Ruby.

Wrapping it up

This was a simple initial / introduction article, but it may save a couple of hours of headaches for developers trying to deploy their code base with unnecessary complexity as I did.

From here on out I’ll be frequently submitting new articles. They will mostly be related to Ruby, (App/Web/OS) server technologies, open source and perhaps various (actual) community related discussions/thoughts.

So if that interests you, be sure to keep checking back!

To Archive → Share twitter

Michael van Rooijen

Crafter of Ruby apps, and open source contributor.

I built and run HireFire.