119

Why am i getting this error?

shibly@mybox:~/blog$ rails server
/home/shibly/.gem/ruby/2.2.4/gems/bundler-1.11.2/lib/bundler/runtime.rb:80:in `rescue in block (2 levels) in require': There was an error while trying to load the gem 'uglifier'. (Bundler::GemRequireError)
    from /home/shibly/.gem/ruby/2.2.4/gems/bundler-1.11.2/lib/bundler/runtime.rb:76:in `block (2 levels) in require'
    from /home/shibly/.gem/ruby/2.2.4/gems/bundler-1.11.2/lib/bundler/runtime.rb:72:in `each'
    from /home/shibly/.gem/ruby/2.2.4/gems/bundler-1.11.2/lib/bundler/runtime.rb:72:in `block in require'
    from /home/shibly/.gem/ruby/2.2.4/gems/bundler-1.11.2/lib/bundler/runtime.rb:61:in `each'
    from /home/shibly/.gem/ruby/2.2.4/gems/bundler-1.11.2/lib/bundler/runtime.rb:61:in `require'
    from /home/shibly/.gem/ruby/2.2.4/gems/bundler-1.11.2/lib/bundler.rb:99:in `require'
    from /home/shibly/blog/config/application.rb:7:in `<top (required)>'
    from /home/shibly/.gem/ruby/2.2.4/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:78:in `require'
    from /home/shibly/.gem/ruby/2.2.4/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:78:in `block in server'
    from /home/shibly/.gem/ruby/2.2.4/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:75:in `tap'
    from /home/shibly/.gem/ruby/2.2.4/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:75:in `server'
    from /home/shibly/.gem/ruby/2.2.4/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /home/shibly/.gem/ruby/2.2.4/gems/railties-4.2.5/lib/rails/commands.rb:17:in `<top (required)>'
    from bin/rails:9:in `require'
    from bin/rails:9:in `<main>
shibly
  • 11,180
  • 33
  • 97
  • 162

3 Answers3

252

You should run following command to install NodeJS in Ubuntu

sudo apt-get install nodejs

or run this for OSX

brew install nodejs

Uglifier is a JS wrapper and it needs a JS runtime running or JS interpreter. I would choose to install NodeJS.

atiqkhaled
  • 336
  • 3
  • 16
Marcos Serpa
  • 2,741
  • 1
  • 13
  • 6
  • 4
    why it needs js env? – DragonKnight Jan 06 '16 at 23:06
  • 1
    It works :) Thanks, But why we need this nodejs to be installed ? – Alankar More Feb 03 '16 at 06:44
  • 1
    We need a JS Runtime, @AlankarMore ;). So, NodeJS this is usually my choice, mainly because it's built on Chrome's V8 engine (https://developers.google.com/v8/). We have a lot of JS today at Rails. And we need an environment set to run all that stuff. That includes wrappers, runtimes/interpreters... – Marcos Serpa Feb 21 '16 at 18:28
  • 1
    If installing nodejs from source, make sure the binary is installed to `/usr/bin/nodejs` where execjs expects to find it. – Rok Strniša Apr 03 '16 at 15:26
  • @MarcosSerpa Thanks! It works for me, too. And I have a question. Is the Nodejs an essential requirement for installing and using Ruby on Rails even when you're not using it directly? – Aboozar Rajabi Mar 28 '17 at 09:34
  • 1
    @AboozarRajabi Nodejs is not an essential, but a JS runtime or interpreter is. And this is the role of NodeJS here, to our case. If not NodeJS, you'll need another JS runtime/interpreter. – Marcos Serpa Mar 28 '17 at 19:29
  • For people struggling with this on Rails 2.4 (where nodejs doesn't solve the problem): http://stackoverflow.com/questions/41461977/after-ruby-2-4-upgrade-error-while-trying-to-load-the-gem-uglifier-bundler – Kasperi May 17 '17 at 06:37
  • Why isn't there a native Ruby JavaScript interpreter? There's one for Java. Then `uglifier` can just depend on that gem, and `bundler` will install everything correctly the first time. – Chloe Nov 02 '17 at 01:23
36

In the default Rails Gemfile, the line for the gem 'therubyracer' is commented out. If you uncomment it, you'll get that gem and it should work.

From "Getting Started with Rails":

Compiling CoffeeScript and JavaScript asset compression requires you have a JavaScript runtime available on your system, in the absence of a runtime you will see an execjs error during asset compilation. Usually Mac OS X and Windows come with a JavaScript runtime installed. Rails adds the therubyracer gem to the generated Gemfile in a commented line for new apps and you can uncomment if you need it. therubyrhino is the recommended runtime for JRuby users and is added by default to the Gemfile in apps generated under JRuby. You can investigate all the supported runtimes at ExecJS.

approxiblue
  • 6,624
  • 16
  • 47
  • 56
user3780968
  • 469
  • 3
  • 3
0

There was an error with creating the db.

The problem is that the Node.js tried to install without sudo in the script:

sudo apt-get install nodejs
General Failure
  • 2,199
  • 3
  • 17
  • 43