Can I run coffeescript in Heroku?

I have a node.js app written in CoffeeScript.

I'm wondering what is needed in order to host the app on Heroku.

Thanks


Solution 1:

Michael Blume is right and you don't need any extra code to run CoffeeScript node apps on heroku. This is how I did it:

Add the coffee-script in the current version to your dependencies in package.json. This might look somewhat like this:

{
  "name": "My-CoffeeScript-App-on-Heroku",
  "version": "0.0.1",
  "dependencies": {
    "coffee-script": "1.1.2"
  }
}

Then modify the entry for your node app in the Procfile to use coffee instead of node. For an app with only a single web entry this might look like this

web: coffee app.coffee

To test if this will work on Heroku, you can try it on localhost using the foreman gem:

$ gem install foreman
$ foreman start
21:13:36 web.1     | started with pid 4711

Then try a push to heroku and you will see something like this in the dependency installation:

-----> Installing dependencies with npm 1.0.8
       [email protected] ./node_modules/coffee-script 
       [email protected] ./node_modules/jade 
       ├── [email protected]
       └── [email protected]

Not sure if there are issues with that procedure but the method described above seems like overkill to me since you're messing up your code for runtime environment stuff.

Hope this helps somebody :)

Solution 2:

I was able to get along fine by just including coffeescript in my dependencies and then putting 'coffee index.coffee' in my Procfile

There's a startup cost to compiling each time your server boots, but other than that you should be fine.

Solution 3:

I got it working by including coffee-script in my package.json and adding node_modules/coffee-script/bin to my Heroku PATH

Solution 4:

Due to the updates with Heroku, it now allows for an npm installation of the coffee-script source. The answer below was a work-around before Heroku fully supported node.js. For a better solution currently, please see the higher rated answer explaining how to simply use coffee-script from npm on Heroku.


To be honest the best way would be to compile it before hand using coffee -c filename like Peter suggested, but I wonder if you could have a sort of 'preload' preload.js that will call the scripts using coffeescript as a node_module then compile() the script to be used. That way you can use them natively in node on heroku without dealing with extra files in your repo.

npm install coffee-script

Then in the inital app, write it in javascript and call the *.coffee files using coffee's compile function:

var coffee = require('coffee-script');
coffee.compile('./server.coffee');
// could be coffee.run(file) instead, not sure

and in yourapp.coffee try

console.log 'It worked!'

I'm not sure if this would work, or if that's even the proper syntax for that function. https://github.com/jashkenas/coffee-script/blob/master/lib/coffee-script.js#L24

If you're asking about doing it in ruby, here's this:

Walkthrough on how to use coffeescript in rails on Heroku: http://drnicwilliams.com/2010/03/15/using-coffeescript-in-rails-and-even-on-heroku/

It suggests using bistro_car ( https://github.com/jnicklas/bistro_car )

gem install bistro_car
mkdir -p app/scripts

and adding it to your Rails conf/environment.rb

config.gem 'bistro_car'

If I find something else or another way to natively run *.coffee javascript apps, I'll update this answer but hopefully this will give you some idea on how to get it to work.

Here are a couple more examples, but they all seem to be using ruby vs node.js as well:

http://forrst.com/posts/Doing_CoffeeScript_on_Heroku_a_Ruby_gem-OBk http://www.tangiblecolors.com/first-steps-with-coffeescript-and-how-to-use

Hope this helps a little bit.