概要
Rails 3.1で新しいプロジェクトを作ったところ、本番環境でJavaScript runtimeが見つからない
というエラーが発生しました。対策として、3.1から導入されたCoffeeScriptのために、JavaScriptエンジンをインストールしました。
Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes.
Rails 3.1からは実行環境にJavaScriptエンジンが必須になったようです。
問題
もともとはブラウザに表示されたInternal Server Errorを調査していて、プロダクションログに次のエラーメッセージを発見したのですが、
Started GET "/working_records" for 197.8.10.19 at 2011-09-20 01:26:07 +0900 ActionController::RoutingError (No route matches [GET] "/working_records"):
ルーティング情報を確認するためにrakeコマンドを実行したところ、次のエラーメッセージが表示されました。
# bundle exec rake routes rake aborted! Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes.
解決方法
Gemfileにexecjs*1とtherubyracer*2を追記して、インストールしました。
FILE: $RAILS_ROOT/Gemfile gem 'execjs' gem 'therubyracer'
therubyracerを選んだのは、たまたまです。execjsが対応するJavaScriptエンジンには、Google V8以外にも以下のようなものがあるので、このうちのいずれかを選べばいいでしょう。
ExecJS lets you run JavaScript code from Ruby. It automatically picks the best runtime available to evaluate your JavaScript program, then returns the result to you as a Ruby object.
ExecJS supports these runtimes:
therubyracer - Google V8 embedded within Ruby
sstephenson/execjs · GitHub
therubyrhino - Mozilla Rhino embedded within JRuby
Johnson - Mozilla SpiderMonkey embedded within Ruby
Mustang - Mustang V8 embedded within Ruby
Node.js
Apple JavaScriptCore - Included with Mac OS X
Mozilla SpiderMonkey
Microsoft Windows Script Host (JScript)
*1:ただし、この時点ですでにexecjsはインストールされていました。
*2:Google V8 Javascript EngineをRubyからネイティブに呼び出すためのライブラリです。