All posts

Fix Missing Import in Ruby

Resolve LoadError and NameError in Ruby scripts by fixing require statements, gem installations, and $LOAD_PATH configuration.

Missing Import Errors in Ruby

Ruby's LoadError: cannot load such file and NameError: uninitialized constant indicate a missing require or an uninstalled gem.

require vs require_relative

# require — searches $LOAD_PATH and gems
require 'json'
require 'httparty'

# require_relative — relative to current file
require_relative 'lib/helpers'
require_relative '../models/user'

Install the Gem

# Direct install
gem install httparty

# Or add to Gemfile
# Gemfile
gem 'httparty'

bundle install

Bundler Require

With Bundler, use Bundler.require to auto-require all gems:

require 'bundler/setup'
Bundler.require

Or require gems individually:

require 'bundler/setup'
require 'sinatra'
require 'sequel'

$LOAD_PATH

Add directories to the load path:

$LOAD_PATH.unshift(File.join(__dir__, 'lib'))
require 'my_module'

Standard Library Requires

Some stdlib modules need explicit require:

require 'net/http'    # HTTP client
require 'uri'         # URL parsing
require 'csv'         # CSV parsing
require 'fileutils'   # File operations
require 'open3'       # Process spawning
require 'set'         # Set data structure

Gem Name vs Require Name

Sometimes they differ:

# gem install aws-sdk-s3
require 'aws-sdk-s3'  # Same

# gem install pg
require 'pg'          # Same

# gem install activerecord
require 'active_record'  # Different!

Bugsly captures LoadError exceptions with the attempted file path, making it immediately clear which gem or file is missing in your production environment.

Try Bugsly Free

AI-powered error tracking that explains your bugs. Set up in 2 minutes, free forever for small projects.

Get Started Free