Ask A Question

Notifications

You’re not receiving notifications from this thread.

Excluding Assets Pipeline gems while running Rails in production

Alex Musayev asked in Rails

I've been thinking, since assets compillation suppose to be performed during production deployment, it is irrelevant to make Bundler to require uglifier, sass-rails, therubyracer and may be some other gems while running Rails on production environment. They will be never used anyway, because config.assets.compile is explicitly set to false in config/environments/production.rb.

What could be a good approach to tell Bundler not to require them? At the moment I'm thinking to use environment variable. Something like this:

gem 'uglifier', require: (ENV['RAILS_ENV'].to_s.downcase != 'production')

May be there could be better options?

Reply

I believe these gems are required to be in the production group because when the assets precompile command runs in the production env. Your assets.compile flag is only for assets that are referenced but uncompiled which isn't likely to happen because you should have them precompiled already.

Plus once the precompile is done, saving 3 requires in your gem will only save you a couple milliseconds on boot only, which only runs when you restart your app, not every request so you aren't likely to save any valuable time doing this anyways.

Reply

Tend to agree about keeping these gems in production after some experiments. Regardless asset pipeline is not directly used in production (in case frontend assets are precompiled, as they should be), removing these gems could cause less stable behavior in some situations. So may be saving some memory by removing them from Gemfile — which way my original motivation — is not worth it.

And I agree, RAILS_ENV was not the right choice in the original post. In the end I was trying to use different conditions to exclude asset pipeline gems from gemfile.

Reply

The only question left is about therubyracer. Documentation says, it is possible to use alternative JS runtime, and keep gem 'therubyracer' line commented in Gemfile, as it is by default. But I keep getting execjs exception saying JS runtime not found, regardless nodejs is installed.

Reply

@alex On the execJS stuff can you post a stacktrace from your capistrano.log? I seem to remember running into this before.

Reply

@shakycode Here is stderr after rails assets:precompile. The weird thing is that I've just tried to reproduce it locally, and didn't get this error (with therubyracer gem is excluded from Gemfile).

Upd: oops sorry, that was wrong stack trace. Here is correct one:

rails aborted!
Bundler::GemRequireError: There was an error while trying to load the gem 'uglifier'.
Gem Load Error is: Could not find a JavaScript runtime. See https://github.com/rails/execjs for a list of available runtimes.
Backtrace for gem load error is:
/var/www/app/shared/bundle/ruby/2.3.0/gems/execjs-2.7.0/lib/execjs/runtimes.rb:58:in `autodetect'
/var/www/app/shared/bundle/ruby/2.3.0/gems/execjs-2.7.0/lib/execjs.rb:5:in `<module:ExecJS>'
/var/www/app/shared/bundle/ruby/2.3.0/gems/execjs-2.7.0/lib/execjs.rb:4:in `<top (required)>'
/var/www/app/shared/bundle/ruby/2.3.0/gems/uglifier-3.0.4/lib/uglifier.rb:5:in `require'
/var/www/app/shared/bundle/ruby/2.3.0/gems/uglifier-3.0.4/lib/uglifier.rb:5:in `<top (required)>'
/home/deploy/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/runtime.rb:91:in `require'
/home/deploy/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/runtime.rb:91:in `block (2 levels) in require'
/home/deploy/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/runtime.rb:86:in `each'
/home/deploy/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/runtime.rb:86:in `block in require'
/home/deploy/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/runtime.rb:75:in `each'
/home/deploy/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/runtime.rb:75:in `require'
/home/deploy/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler.rb:106:in `require'
/var/www/app/releases/_/config/application.rb:5:in `<top (required)>'
/var/www/app/releases/_/Rakefile:4:in `require'
/var/www/app/releases/_/Rakefile:4:in `<top (required)>'
/var/www/app/shared/bundle/ruby/2.3.0/gems/rake-11.3.0/lib/rake/rake_module.rb:28:in `load'
/var/www/app/shared/bundle/ruby/2.3.0/gems/rake-11.3.0/lib/rake/rake_module.rb:28:in `load_rakefile'
/var/www/app/shared/bundle/ruby/2.3.0/gems/rake-11.3.0/lib/rake/application.rb:686:in `raw_load_rakefile'
/var/www/app/shared/bundle/ruby/2.3.0/gems/rake-11.3.0/lib/rake/application.rb:96:in `block in load_rakefile'
/var/www/app/shared/bundle/ruby/2.3.0/gems/rake-11.3.0/lib/rake/application.rb:178:in `standard_exception_handling'
/var/www/app/shared/bundle/ruby/2.3.0/gems/rake-11.3.0/lib/rake/application.rb:95:in `load_rakefile'
/var/www/app/shared/bundle/ruby/2.3.0/gems/railties-5.0.1/lib/rails/commands/rake_proxy.rb:13:in `block in run_rake_task'
/var/www/app/shared/bundle/ruby/2.3.0/gems/rake-11.3.0/lib/rake/application.rb:178:in `standard_exception_handling'
/var/www/app/shared/bundle/ruby/2.3.0/gems/railties-5.0.1/lib/rails/commands/rake_proxy.rb:11:in `run_rake_task'
/var/www/app/shared/bundle/ruby/2.3.0/gems/railties-5.0.1/lib/rails/commands/commands_tasks.rb:51:in `run_command!'
/var/www/app/shared/bundle/ruby/2.3.0/gems/railties-5.0.1/lib/rails/commands.rb:18:in `<top (required)>'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Bundler Error Backtrace:
/home/deploy/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/runtime.rb:94:in `rescue in block (2 levels) in require'
/home/deploy/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/runtime.rb:90:in `block (2 levels) in require'
/home/deploy/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/runtime.rb:86:in `each'
/home/deploy/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/runtime.rb:86:in `block in require'
/home/deploy/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/runtime.rb:75:in `each'
/home/deploy/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/runtime.rb:75:in `require'
/home/deploy/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler.rb:106:in `require'
/var/www/app/releases/_/config/application.rb:5:in `<top (required)>'
/var/www/app/releases/_/Rakefile:4:in `require'
/var/www/app/releases/_/Rakefile:4:in `<top (required)>'
/var/www/app/shared/bundle/ruby/2.3.0/gems/rake-11.3.0/lib/rake/rake_module.rb:28:in `load'
/var/www/app/shared/bundle/ruby/2.3.0/gems/rake-11.3.0/lib/rake/rake_module.rb:28:in `load_rakefile'
/var/www/app/shared/bundle/ruby/2.3.0/gems/rake-11.3.0/lib/rake/application.rb:686:in `raw_load_rakefile'
/var/www/app/shared/bundle/ruby/2.3.0/gems/rake-11.3.0/lib/rake/application.rb:96:in `block in load_rakefile'
/var/www/app/shared/bundle/ruby/2.3.0/gems/rake-11.3.0/lib/rake/application.rb:178:in `standard_exception_handling'
/var/www/app/shared/bundle/ruby/2.3.0/gems/rake-11.3.0/lib/rake/application.rb:95:in `load_rakefile'
/var/www/app/shared/bundle/ruby/2.3.0/gems/railties-5.0.1/lib/rails/commands/rake_proxy.rb:13:in `block in run_rake_task'
/var/www/app/shared/bundle/ruby/2.3.0/gems/rake-11.3.0/lib/rake/application.rb:178:in `standard_exception_handling'
/var/www/app/shared/bundle/ruby/2.3.0/gems/railties-5.0.1/lib/rails/commands/rake_proxy.rb:11:in `run_rake_task'
/var/www/app/shared/bundle/ruby/2.3.0/gems/railties-5.0.1/lib/rails/commands/commands_tasks.rb:51:in `run_command!'
/var/www/app/shared/bundle/ruby/2.3.0/gems/railties-5.0.1/lib/rails/commands.rb:18:in `<top (required)>'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
ExecJS::RuntimeUnavailable: Could not find a JavaScript runtime. See https://github.com/rails/execjs for a list of available runtimes.
/var/www/app/shared/bundle/ruby/2.3.0/gems/execjs-2.7.0/lib/execjs/runtimes.rb:58:in `autodetect'
/var/www/app/shared/bundle/ruby/2.3.0/gems/execjs-2.7.0/lib/execjs.rb:5:in `<module:ExecJS>'
/var/www/app/shared/bundle/ruby/2.3.0/gems/execjs-2.7.0/lib/execjs.rb:4:in `<top (required)>'
/var/www/app/shared/bundle/ruby/2.3.0/gems/uglifier-3.0.4/lib/uglifier.rb:5:in `require'
/var/www/app/shared/bundle/ruby/2.3.0/gems/uglifier-3.0.4/lib/uglifier.rb:5:in `<top (required)>'
/home/deploy/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/runtime.rb:91:in `require'
/home/deploy/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/runtime.rb:91:in `block (2 levels) in require'
/home/deploy/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/runtime.rb:86:in `each'
/home/deploy/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/runtime.rb:86:in `block in require'
/home/deploy/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/runtime.rb:75:in `each'
/home/deploy/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/runtime.rb:75:in `require'
/home/deploy/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler.rb:106:in `require'
/var/www/app/releases/_/config/application.rb:5:in `<top (required)>'
/var/www/app/releases/_/Rakefile:4:in `require'
/var/www/app/releases/_/Rakefile:4:in `<top (required)>'
/var/www/app/shared/bundle/ruby/2.3.0/gems/rake-11.3.0/lib/rake/rake_module.rb:28:in `load'
/var/www/app/shared/bundle/ruby/2.3.0/gems/rake-11.3.0/lib/rake/rake_module.rb:28:in `load_rakefile'
/var/www/app/shared/bundle/ruby/2.3.0/gems/rake-11.3.0/lib/rake/application.rb:686:in `raw_load_rakefile'
/var/www/app/shared/bundle/ruby/2.3.0/gems/rake-11.3.0/lib/rake/application.rb:96:in `block in load_rakefile'
/var/www/app/shared/bundle/ruby/2.3.0/gems/rake-11.3.0/lib/rake/application.rb:178:in `standard_exception_handling'
/var/www/app/shared/bundle/ruby/2.3.0/gems/rake-11.3.0/lib/rake/application.rb:95:in `load_rakefile'
/var/www/app/shared/bundle/ruby/2.3.0/gems/railties-5.0.1/lib/rails/commands/rake_proxy.rb:13:in `block in run_rake_task'
/var/www/app/shared/bundle/ruby/2.3.0/gems/rake-11.3.0/lib/rake/application.rb:178:in `standard_exception_handling'
/var/www/app/shared/bundle/ruby/2.3.0/gems/railties-5.0.1/lib/rails/commands/rake_proxy.rb:11:in `run_rake_task'
/var/www/app/shared/bundle/ruby/2.3.0/gems/railties-5.0.1/lib/rails/commands/commands_tasks.rb:51:in `run_command!'
/var/www/app/shared/bundle/ruby/2.3.0/gems/railties-5.0.1/lib/rails/commands.rb:18:in `<top (required)>'
bin/rails:4:in `require'
bin/rails:4:in `<main>'", 
    "stdout": "", 
    "stdout_lines": [], 
    "warnings": []
Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 82,464+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.