Home > other >  Why am I getting a 'NameError: uninitialized constant Dotenv' when deploying to Heroku?
Why am I getting a 'NameError: uninitialized constant Dotenv' when deploying to Heroku?

Time:10-20

I'm trying to deploy a simple Ruby app to Heroku. I keep getting this error in the release logs, which look like this:

rake aborted!
NameError: uninitialized constant Dotenv
/app/config/environment.rb:5:in `<top (required)>'
/app/Rakefile:3:in `require_relative'
/app/Rakefile:3:in `<top (required)>'
/app/vendor/bundle/ruby/2.7.0/gems/rake-13.0.1/exe/rake:27:in `<top (required)>'
/app/vendor/bundle/ruby/2.7.0/gems/bundler-2.2.21/lib/bundler/cli/exec.rb:63:in `load'
/app/vendor/bundle/ruby/2.7.0/gems/bundler-2.2.21/lib/bundler/cli/exec.rb:63:in `kernel_load'
/app/vendor/bundle/ruby/2.7.0/gems/bundler-2.2.21/lib/bundler/cli/exec.rb:28:in `run'
/app/vendor/bundle/ruby/2.7.0/gems/bundler-2.2.21/lib/bundler/cli.rb:474:in `exec'
/app/vendor/bundle/ruby/2.7.0/gems/bundler-2.2.21/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/app/vendor/bundle/ruby/2.7.0/gems/bundler-2.2.21/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
/app/vendor/bundle/ruby/2.7.0/gems/bundler-2.2.21/lib/bundler/vendor/thor/lib/thor.rb:392:in `dispatch'
/app/vendor/bundle/ruby/2.7.0/gems/bundler-2.2.21/lib/bundler/cli.rb:30:in `dispatch'
/app/vendor/bundle/ruby/2.7.0/gems/bundler-2.2.21/lib/bundler/vendor/thor/lib/thor/base.rb:485:in `start'
/app/vendor/bundle/ruby/2.7.0/gems/bundler-2.2.21/lib/bundler/cli.rb:24:in `start'
/app/vendor/bundle/ruby/2.7.0/gems/bundler-2.2.21/exe/bundle:49:in `block in <top (required)>'
/app/vendor/bundle/ruby/2.7.0/gems/bundler-2.2.21/lib/bundler/friendly_errors.rb:128:in `with_friendly_errors'
/app/vendor/bundle/ruby/2.7.0/gems/bundler-2.2.21/exe/bundle:37:in `<top (required)>'
/app/vendor/bundle/bin/bundle:113:in `load'
/app/vendor/bundle/bin/bundle:113:in `<main>'
(See full trace by running task with --trace)

I've googled this a bunch and tried every suggestion, but I can't seem to fix it. For reference, here's my Gemfile:

source 'http://rubygems.org'

gem 'sinatra'
gem 'activerecord', "< 6", :require => 'active_record'
gem 'sinatra-activerecord', :require => 'sinatra/activerecord'
gem 'rake'
gem 'require_all'
gem 'thin'
gem 'bcrypt'
gem 'sinatra-flash'
gem 'rails_12factor'
gem 'foreman'

group :development, :test do 
  gem 'sqlite3', '<1.4'
  gem 'shotgun'
  gem 'tux'
  gem 'pry'
  gem 'session_secret_generator'
  gem 'dotenv'
end

group :production do 
  gem 'pg', '0.20'
end

group :test do
  gem 'rspec'
  gem 'capybara'
  gem 'rack-test'
  gem 'database_cleaner', git: 'https://github.com/bmabey/database_cleaner.git'
end

and here's my config/environment.rb:

ENV['SINATRA_ENV'] ||= "development"

require 'bundler/setup'
Bundler.require(:default, ENV['SINATRA_ENV'])
Dotenv.load if ENV['SINATRA_ENV'] == "development"

set :database_file, "./database.yml"

require './app/controllers/application_controller'
require_all 'app'

Thanks!

CodePudding user response:

Maybe you are missing: require 'dotenv' before trying Dotenv.load?

CodePudding user response:

Problems

Note that your Gemfile places dotenv into your :development and :test groups. It is not available in your default or :production groups, so you are unlikely to be getting your gem loaded via Bundler.require as currently configured.

Suggested Solutions

My suggestions include:

  1. ensure you're using APP_ENV to set your Sinatra environment, not something custom like SINATRA_ENV, which may or may not doing what you think on Heroku
  2. use Heroku's config-vars rather than dotenv in production
  3. note that Heroku probably defaults to excluding :development, :test, and :ci groups when running bundle install, but you can configure this behavior via environment variables
  4. move dotenv to your default group (if you really need it everywhere)
  5. if you don't move dotenv into the default group, you need to modify your Bundler.require to add the correct group containing the dotenv gem into the list of groups to require (provided Bundler has installed that group)

You may have other issues as well, but these are the ones that jumped out at me from the code and errors you provided.

  • Related