Can I run coffeescript in Heroku?

2019-01-30 07:49发布

问题:

I have a node.js app written in CoffeeScript.

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

Thanks

回答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.



回答2:

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.

Hope this helps somebody :)



回答3:

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.



回答4:

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



回答5:

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.



回答6:

  • Add coffee-script to your package.json
  • Change your Profile to web: coffee app.coffee

See florian.k's answer



回答7:

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.