[Ruby] When you get LoadError (cannot load such file –ruby_file) when you require in Ruby

2 minute read

Introduction

I will write about the story when LoadError (cannot load such file --your_ruby_file_here)

This case

Ruby version

$ ruby -v
ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-darwin19]

This is the case of folder structure

$ tree
.
├── module.rb
└── sub_modules
    ├── sub_module_a.rb
    ├── sub_module_b.rb
    └── sub_module_c.rb

module.rb


require 'sub_modules/sub_module_a'
require 'sub_modules/sub_module_b'
require 'sub_modules/sub_module_c'

module MainModule
  Version = "2.3.0"
end

sub_modules/sub_module_a.rb


module SubModuleA
 Version = "1.0.0"
end

Attempting to load module.rb from main in this situation may result in an error

require or require_relative

irb(main):004:0> require 'module'
Traceback (most recent call last):
        7: from /Users/masaino/.rbenv/versions/2.7.1/bin/irb:23:in `<main>'
        6: from /Users/masaino/.rbenv/versions/2.7.1/bin/irb:23:in `load'
        5: from /Users/masaino/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/irb-1.2.3/exe/irb:11:in `<top (required)>'
        4: from (irb):3
        3: from (irb):4:in `rescue in irb_binding'
        2: from /Users/masaino/.rbenv/versions/2.7.1/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:92:in `require'
        1: from /Users/masaino/.rbenv/versions/2.7.1/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:92:in `require'
LoadError (cannot load such file -- module)

This seems to mean that module.rb cannot be found because the current directory is not in $ LOAD_PATH referenced by require. So add ./ and try as follows

irb(main):006:0> require './module'
Traceback (most recent call last):
       10: from /Users/masaino/.rbenv/versions/2.7.1/bin/irb:23:in `<main>'
        9: from /Users/masaino/.rbenv/versions/2.7.1/bin/irb:23:in `load'
        8: from /Users/masaino/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/irb-1.2.3/exe/irb:11:in `<top (required)>'
        7: from (irb):5
        6: from (irb):6:in `rescue in irb_binding'
        5: from /Users/masaino/.rbenv/versions/2.7.1/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:92:in `require'
        4: from /Users/masaino/.rbenv/versions/2.7.1/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:92:in `require'
        3: from /Users/masaino/load_path/module.rb:1:in `<top (required)>'
        2: from /Users/masaino/.rbenv/versions/2.7.1/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:92:in `require'
        1: from /Users/masaino/.rbenv/versions/2.7.1/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:92:in `require'
LoadError (cannot load such file -- sub_module_a)

The error message has changed to LoadError (cannot load such file --sub_module_a). The module itself can be loaded, but in this case it seems to be messed up because main.rb further requires the sub_module.

There is also require_relative as a” require with a relative path from the current file “, but the result is the same.

irb(main):005:0> require_relative 'module'
Traceback (most recent call last):
        9: from /Users/masaino/.rbenv/versions/2.7.1/bin/irb:23:in `<main>'
        8: from /Users/masaino/.rbenv/versions/2.7.1/bin/irb:23:in `load'
        7: from /Users/masaino/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/irb-1.2.3/exe/irb:11:in `<top (required)>'
        6: from (irb):4
        5: from (irb):5:in `rescue in irb_binding'
        4: from (irb):5:in `require_relative'
        3: from /Users/masaino/load_path/module.rb:1:in `<top (required)>'
        2: from /Users/masaino/.rbenv/versions/2.7.1/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:92:in `require'
        1: from /Users/masaino/.rbenv/versions/2.7.1/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:92:in `require'
LoadError (cannot load such file -- sub_module_a)

$LOAD_PATH

So, it seems better to set the current directory to $ LOAD_PATH obediently. What’s happening with the current $ LOAD_PATH in the first place?

irb(main):001:0> puts $LOAD_PATH
/usr/local/Cellar/rbenv/1.1.2/rbenv.d/exec/gem-rehash
/Users/masaino/.rbenv/versions/2.7.1/lib/ruby/site_ruby/2.7.0
/Users/masaino/.rbenv/versions/2.7.1/lib/ruby/site_ruby/2.7.0/x86_64-darwin19
/Users/masaino/.rbenv/versions/2.7.1/lib/ruby/site_ruby
/Users/masaino/.rbenv/versions/2.7.1/lib/ruby/vendor_ruby/2.7.0
/Users/masaino/.rbenv/versions/2.7.1/lib/ruby/vendor_ruby/2.7.0/x86_64-darwin19
/Users/masaino/.rbenv/versions/2.7.1/lib/ruby/vendor_ruby
/Users/masaino/.rbenv/versions/2.7.1/lib/ruby/2.7.0
/Users/masaino/.rbenv/versions/2.7.1/lib/ruby/2.7.0/x86_64-darwin19
=> nil

Certainly there is no current directory so I will add it

irb(main):002:0> $LOAD_PATH.push('.')
=> ["/usr/local/Cellar/rbenv/1.1.2/rbenv.d/exec/gem-rehash", "/Users/masaino/.rbenv/versions/2.7.1/lib/ruby/site_ruby/2.7.0", "/Users/masaino/.rbenv/versions/2.7.1/lib/ruby/site_ruby/2.7.0/x86_64-darwin19", "/Users/masaino/.rbenv/versions/2.7.1/lib/ruby/site_ruby", "/Users/masaino/.rbenv/versions/2.7.1/lib/ruby/vendor_ruby/2.7.0", "/Users/masaino/.rbenv/versions/2.7.1/lib/ruby/vendor_ruby/2.7.0/x86_64-darwin19", "/Users/masaino/.rbenv/versions/2.7.1/lib/ruby/vendor_ruby", "/Users/masaino/.rbenv/versions/2.7.1/lib/ruby/2.7.0", "/Users/masaino/.rbenv/versions/2.7.1/lib/ruby/2.7.0/x86_64-darwin19", "."]

When I loaded it again, it worked: thumbsup:

irb(main):006:0> require 'module'
=> true
irb(main):003:0> MainModule::Version
=> "2.3.0"
irb(main):004:0> SubModuleA::Version
=> "1.0.0"

Referenced site

-Check the path when Ruby loads the library
-How to set $ LOAD_PATH

Tags:

Updated: