15

I created a rails application, and tried to start the rails server. I got following error:

/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>'

although I was able to fix it by removing the comment before the line gem 'therubyracer', platforms: :ruby from Gemfile.

Why is this gem commented out when we require it at very first place when starting rails application?

Ritesh Kumar
  • 2,183
  • 2
  • 20
  • 32
  • Very good Question, in addition, in rails > 4.0 the error message is not helpful either. `There was an error while trying to load the gem 'uglifier'. (Bundler::GemRequireError)` – Shiva Mar 05 '16 at 01:45

1 Answers1

22

Because if another JavaScript runtime (like Node.js) is installed on the machine, Rails can use that, and therubyracer is not required.

Rails uses ExecJS to load a JavaScript runtime. For more information on ExecJS, see its Github page: https://github.com/sstephenson/execjs. The readme provides a list of usable runtimes (as your error log notes).

Depending on your deployment process and your production environment, it may not really matter which JavaScript runtime you use. Some people have issues with the size of the therubyracer library (see Rails - Could not find a JavaScript runtime?), and it may not be smart to install it on your production server.

Community
  • 1
  • 1
Jacob Brown
  • 7,221
  • 4
  • 30
  • 50
  • is there relevant docs for when one should be avoiding `therubyracer` in production? I.e. under what traffic, memory constraints etc. – Don Cheadle Mar 02 '15 at 15:40
  • 1
    I have a Node environment setup through nvm and still Rails failed with that cryptic error. Took me sometime to realize I needed to uncomment that line... I think a better option for Rails default would be to opt-out of it instead of opt-in. – zenw0lf Oct 22 '16 at 13:49
  • Any update on this I'm trying to figure out why jquery stopped working after upgrading to Ubuntu 20.04 I'm not getting any errors except ruleset ignored due to bad selector for all @ statements in .scss files – Mices May 22 '21 at 07:55