Matthew James Briggs
Matthew James Briggs

Reputation: 2265

Rails Require `No Such File to Load` Error When Running in Production

My rails app works when running locally in development mode. When I try to run it in production mode, I get this problem both locally and when I tried to set up an AWS Ubuntu server.

RAILS_ENV=production rails s
=> Booting Puma
=> Rails 5.1.4 application starting in production 
=> Run `rails server -h` for more startup options
Exiting
/usr/local/lib/ruby/gems/2.4.0/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `require': No such file to load -- ../../config/environment.rb (LoadError)

I can't understand exactly what that relative path is relative to. There is a file at myrailsapp/config/environment.rb which states require_relative 'application'. There is a file in the same directory named application.rb.

I can't understand what ruby is looking for, or why it cannot find it.

Upvotes: 0

Views: 2521

Answers (1)

EJAg
EJAg

Reputation: 3298

Your app is trying to load ../../config/environment.rb but couldn't find it. It seems to be specific to your production env, so probably set in your /config/environment/production.rb or some other production-specific configurations/files, which are initialized at startup.

Another problem is that #require needs an absolute path. For creating absolute path from relative paths, use

require_relative "../config/environment"

or something like

require File.expand_path('../../config/environment', __FILE__)

Edit: with some more specifics. The offensive require '../../config/environment' was in a side script of the OP's own making, e.g. it was not a model, view, controller or other file created by Rails and was not referenced by the Rails application. However, it appears the Rails, only in production mode, introspected the rb files in its directories including this custom script and raised the error. The fixes are twofold.

  1. Don't put any custom non-app-specific files in Rails' special folders (such as myapp/app/model, instead create your own directory under the app root for such things.
  2. use require_relative instead of require when specifying a relative path.

Upvotes: 3

Related Questions