ponkiti's blog

主に自分用、イベント参加メモや備忘録として利用

rails sコマンド実行時に「Could not find a JavaScript runtime.」とエラーが出る場合の対処法

$ rails s
/home/vagrant/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/execjs-2.2.2/lib/execjs/runtimes.rb:51:in `autodetect': Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)
     from /home/vagrant/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/execjs-2.2.2/lib/execjs.rb:5:in `<module:ExecJS>'
 (省略)
     from bin/rails:4:in `require'
     from bin/rails:4:in `<main>'

Gemfileにgem 'therubyracer'という記述を追加して、bundle update以降の手順を同様に実行する。

[vagrant@localhost first_app]$ vi Gemfile

[vagrant@localhost first_app]$ bundle update
Fetching gem metadata from https://rubygems.org/..........
Resolving dependencies...
Using rake 10.3.2
(省略)
Using uglifier 2.1.1
Your bundle is updated!

[vagrant@localhost first_app]$ bundle install
Using rake 10.3.2
(省略)
Using uglifier 2.1.1
Your bundle is complete!
Use `bundle show [gemname]` to see where a bundled gem is installed.

[vagrant@localhost first_app]$ rails s
=> Booting WEBrick
=> Rails 4.0.5 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2014-10-16 06:58:34] INFO  WEBrick 1.3.1
[2014-10-16 06:58:34] INFO  ruby 2.0.0 (2013-02-24) [x86_64-linux]
[2014-10-16 06:58:34] INFO  WEBrick::HTTPServer#start: pid=16988 port=3000

一応、Ruby on Railsチュートリアルにも対処法が記載されている。

Ruby on Rails チュートリアル:実例を使って Rails を学ぼう

(JavaScriptランタイムがインストールされていないというエラーが表示された場合は、GitHubのexecjsページにあるインストール可能なランタイムを確認してください。Node.jsが特にお勧めです)。

参考