Monday, September 9, 2013

'ExecJS::RuntimeUnavailable' error after creating new Rails4 application

Create new rails4 application, run command: rails new interview -d mysql
It will automatically run bundle install.
Browse to interview folder and try to start rails server by command, 'rails s'.
It will give error:

ritesh@ritesh-ThinkPad-L420:~/projects/interview$ rails s
/home/ritesh/.rvm/gems/ruby-1.9.3-p429/gems/execjs-2.0.1/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/ritesh/.rvm/gems/ruby-1.9.3-p429/gems/execjs-2.0.1/lib/execjs.rb:5:in `<module:ExecJS>'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p429/gems/execjs-2.0.1/lib/execjs.rb:4:in `<top (required)>'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p429/gems/uglifier-2.2.1/lib/uglifier.rb:3:in `require'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p429/gems/uglifier-2.2.1/lib/uglifier.rb:3:in `<top (required)>'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p429@global/gems/bundler-1.3.5/lib/bundler/runtime.rb:72:in `require'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p429@global/gems/bundler-1.3.5/lib/bundler/runtime.rb:72:in `block (2 levels) in require'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p429@global/gems/bundler-1.3.5/lib/bundler/runtime.rb:70:in `each'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p429@global/gems/bundler-1.3.5/lib/bundler/runtime.rb:70:in `block in require'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p429@global/gems/bundler-1.3.5/lib/bundler/runtime.rb:59:in `each'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p429@global/gems/bundler-1.3.5/lib/bundler/runtime.rb:59:in `require'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p429@global/gems/bundler-1.3.5/lib/bundler.rb:132:in `require'
    from /home/ritesh/projects/interview/config/application.rb:7:in `<top (required)>'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p429/gems/railties-4.0.0/lib/rails/commands.rb:76:in `require'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p429/gems/railties-4.0.0/lib/rails/commands.rb:76:in `block in <top (required)>'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p429/gems/railties-4.0.0/lib/rails/commands.rb:73:in `tap'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p429/gems/railties-4.0.0/lib/rails/commands.rb:73:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

Solution:
To fix this issue, edit Gemfile and comment out the line:
        gem 'therubyracer', platforms: :ruby
After this run bundle install and you can start rails server.

Although I was able to fix it, but I am not sure why 'therubyracer' gem in Gemfile is commented ???


2 comments:

  1. As I understand it, you need a server-side javascript runtime. Installing therubyracer gem as you did is an option, but not the only one. I've seen some benchmarks claiming it's faster to use nodejs in the server, for example.

    The difference is that therubyracer can be installed via bundler as a gem, while nodejs has to be installed with whatever package manager your distro uses (pacman, apt-get, whatever). Also with therubyracer gem you can easily deploy to your staging/production machines using capistrano, and the "bundle install" step in the deployment will install the gem; on the other hand with nodejs or other non-gem solutions you have to install them by hand in each machine.

    Unless you use puppet or something like that, I suppose; I'm not proficient with it so I cannot comment.

    ReplyDelete
    Replies
    1. Thanks Glauco for prompt clarification.

      Delete