There was an error while trying to load the gem 'uglifier'. (Bundler::GemRequireError)

77,753

Solution 1

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.

Solution 2

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.

Share:
77,753
shibly
Author by

shibly

Updated on March 29, 2020

Comments

  • shibly
    shibly about 4 years

    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>
    
  • DragonKnight
    DragonKnight over 8 years
    why it needs js env?
  • Alankar More
    Alankar More about 8 years
    It works :) Thanks, But why we need this nodejs to be installed ?
  • Marcos Serpa
    Marcos Serpa about 8 years
    We need a JS Runtime, @AlankarMore ;). So, NodeJS this is usually my choice, mainly because it's built on Chrome's V8 engine (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...
  • Rok Strniša
    Rok Strniša about 8 years
    If installing nodejs from source, make sure the binary is installed to /usr/bin/nodejs where execjs expects to find it.
  • ConnorWGarvey
    ConnorWGarvey over 7 years
    Thanks! This was easier and seems cleaner than relying on NodeJS.
  • Aboozar Rajabi
    Aboozar Rajabi about 7 years
    @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?
  • Marcos Serpa
    Marcos Serpa about 7 years
    @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.
  • Cyril Duchon-Doris
    Cyril Duchon-Doris about 7 years
    While this answer works it eludes that you should rather have (for production) a proper execjs runtime like node.js
  • Kasperi
    Kasperi almost 7 years
    For people struggling with this on Rails 2.4 (where nodejs doesn't solve the problem): stackoverflow.com/questions/41461977/…
  • Chloe
    Chloe over 6 years
    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.