/errors/ruby/uninitialized-constant
Ruby: uninitialized constant Name (NameError)
The commands on this page have not been executed by our verification runner yet.updated
- tool
- Ruby
- exit code
- 1
- versions
- Ruby 2.x, Ruby 3.x, Rails 6+ (Zeitwerk)
- os
- any
uninitialized constant Name (NameError) means Ruby reached a reference to Name, a class, module or constant, and no definition was loaded under that name in the current namespace. In a plain Ruby script the fix is usually a missing require / require_relative or a typo. In Rails it usually means the file name does not match the class name, so the autoloader (Zeitwerk) never loads it; the message then shows the namespace where Ruby looked, for example uninitialized constant Admin::UsersController::User.
Example
puts Invoice.new.total # invoice.rb was never requiredmain.rb:1:in '<main>': uninitialized constant Invoice (NameError)Fixes
require_relative 'invoice' # load the file that defines Invoice
require 'json' # constants from the standard library or gems need require too
puts JSON.generate({ ok: true })
module Admin
class Report
def user = ::User.first # :: forces the top-level User, not Admin::User
end
endRails file names
| File | Must define |
|---|---|
| app/models/user.rb | User |
| app/models/user_account.rb | UserAccount |
| app/models/admin/user.rb | Admin::User |
| app/services/pdf_export.rb | PdfExport (or configure the PDF acronym in an inflector) |
bin/rails zeitwerk:check # lists files whose names do not match their constantsSources
Related pages
errors · rubyRuby: cannot load such file -- name (LoadError)Ruby raises LoadError when require finds no file: the gem is not installed for this Ruby or not in the Gemfile, or a local file needs require_relative instead.errors · rubyRuby NoMethodError: undefined method for nilRuby raises NoMethodError undefined method for nil when a method is called on a value that is nil, often a failed lookup. Find the nil source; use &. or fetch.commands · bundlerbundle exec: run a command with the Gemfile's gem versionsbundle exec CMD runs a command with exactly the gem versions in Gemfile.lock, avoiding conflicts with other installed versions. When you need it, and binstubs.