50

I have a node.js app written in CoffeeScript.

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

Thanks

om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
donald
  • 23,587
  • 42
  • 142
  • 223
  • 1
    As far as I know, heroku only hosts ruby applications. – Ikke Jun 15 '11 at 10:26
  • They added it recently. Note that if you want to run CoffeeScript in a Rails app on Heroku, just add the latest `therubyracer` to your Gemfile and you should be set. – Trevor Burnham Jun 15 '11 at 15:06
  • 1
    I suggest you accept florian.k's answer, it's much more straightforward than the currently accepted one. – kynan May 21 '12 at 00:26
  • Shouldn't you develop in coffeescript and deploy in compiled JS? What if heroku's compiler version is different from what is on your dev machine. It would be nightmare considering what all can go wrong by pushing the compile step to a production server. – iankit Oct 08 '15 at 09:31

7 Answers7

117

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
       coffee-script@1.1.2 ./node_modules/coffee-script 
       jade@0.15.3 ./node_modules/jade 
       ├── mkdirp@0.0.6
       └── commander@0.1.0

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.

starball
  • 20,030
  • 7
  • 43
  • 238
floriankrueger
  • 2,003
  • 2
  • 16
  • 25
  • 11
    CoffeeScript is not globally installed, so you should use the locally installed. Update your `Procfile` like: `web: node_modules/coffee-script/bin/coffee app.coffee` – Koen. Jul 11 '12 at 08:42
  • This is the correct answer. I've got an example of a coffescript/nodejs app that does all that and integrates nodemon for dev purposes... http://github.com/lookfirst/convert – Public Profile Oct 07 '12 at 04:15
  • 4
    I recently verified this again and wanted to share the example code with you: https://github.com/floriankrueger/heroku-on-coffee – floriankrueger Oct 11 '12 at 21:43
  • This also works for `LiveScript`, just put `LiveScript` in your dependencies, and in your `Procfile` put `web: lsc app.ls`. – Andriy Drozdyuk Jan 15 '13 at 16:41
  • Even shorter: `web: node_modules/.bin/coffee app.coffee` – superlukas Oct 31 '13 at 14:32
  • Does this approach mean you get correct line numbers in stack traces for errors thrown from your `*.coffee` files? (As opposed to line numbers of the compiled JS...) – callum Dec 11 '13 at 12:21
  • While I like the simplicity of this answer, using `coffee` as the executable in `Procfile` may mean that CoffeeScript compilation occurs upon each and every request due to Heroku's read-only file system. If this is the case it would make it an undesirable solution in my view. – GregT Jan 11 '14 at 21:54
9

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.

MichaelBlume
  • 228
  • 1
  • 6
2

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

1

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.

slickplaid
  • 1,411
  • 1
  • 17
  • 20
  • I think Slick's on the right track. You don't need any binaries to run `.coffee` files; you just need the `coffee-script` library, which is pure JS. Indeed, the `coffee` binary is implemented as pure JS (it wraps around CoffeeScript's `command.js`). – Trevor Burnham Jun 15 '11 at 15:09
  • As to some of the specifics of the post: You should use `coffee.run`; `coffee.compile` just gives you the JS output as a string. Dr. Nic's post on using CoffeeScript on Rails with Heroku is outdated; you should be using either Rails 3.1 or [Barista](https://github.com/sutto/barista), not bistro_car (which hasn't been updated in over a year and, among other things, doesn't support Rails 3). – Trevor Burnham Jun 15 '11 at 15:30
  • I`ve just made a sample of it: https://github.com/lfreneda/how-to-coffeescript-on-heroku – Luiz Freneda Dec 01 '15 at 17:56
0
  • Add coffee-script to your package.json
  • Change your Profile to web: coffee app.coffee

See florian.k's answer

Community
  • 1
  • 1
Dorian
  • 22,759
  • 8
  • 120
  • 116
0

There's been a custom buildpack around for quite some time now, by Chris Fung. I've been using it for a couple of years, until recently when it stopped working with the new Cedar-14 Stack on Heroku. So, I modified Chris' buildpack and you can now use this new custom buildpack to run coffeescript apps on Heroku.

Shiprack
  • 1,095
  • 11
  • 18
0

I googled around but it seems unclear. Here's the heroku guide, which doesn't mention coffeescript. http://devcenter.heroku.com/articles/node-js

I think you can just run coffee -c . in your app`s git repo before you commit and push to heroku (script this as part of your deploy script), and then just use the .js code compiled by that process.

Peter Lyons
  • 142,938
  • 30
  • 279
  • 274