Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NameError: uninitialized constant Dotenv while pushing rails app to heroku

I'm using dotenv to store environment variables and ever since I include it in the gemfile I cannot push it to heroku. I'm getting the following error:-

remote: -----> Installing node-v6.10.0-linux-x64
remote: -----> Detecting rake tasks
remote: sh: 2: Syntax error: Unterminated quoted string
remote: sh: 2: Syntax error: Unterminated quoted string
remote:  !
remote:  !     Could not detect rake tasks
remote:  !     ensure you can run `$ bundle exec rake -P` against your app
remote:  !     and using the production group of your Gemfile.
remote:  !     rake aborted!
remote:  !     NameError: uninitialized constant Dotenv
remote:  !     /tmp/build_5437bc300afb80cfa46b1111bb960f46/config/application.rb:17:in `<top (required)>'
remote:  !     /tmp/build_5437bc300afb80cfa46b1111bb960f46/Rakefile:4:in `require_relative'
remote:  !     /tmp/build_5437bc300afb80cfa46b1111bb960f46/Rakefile:4:in `<top (required)>'

This is how I'm including dotenv in my gemfile:-

gem 'dotenv-rails', :require => 'dotenv/rails-now'

I have tried adding the following in the application.rb file as well:-

Bundler.require(*Rails.groups)

Dotenv::Railtie.load

HOSTNAME = ENV['HOSTNAME']

still doesn't work.

I don't know if those two lines that say "unterminated quoted string" could be some unrelated issue leading to the dotenv not loading. I looked it up and checked heroku config to see if there was something amiss in the variables but they all seem fine. I was able to push before I added the dotenv to gemfile.

I tried running bundle install, restarting server, deleting gemfile.lock and running bundle install and I looked this issue up on here and tried solutions suggested in Can't push to Heroku because of DOTENV uninitialized constant error

Still no luck.

PS - I'm trying to implement recaptcha and it is suggested best practice to use dotenv to store the site_key and secret_key for recaptcha as env vars. Hence I'm trying to get this to work.

like image 525
Ragav Y Avatar asked Jun 14 '17 13:06

Ragav Y


1 Answers

I faced the same issue and was able to solve it with a following.

1) Add dotenv-rails in the Gemfile only for specific environments:

# Gemfile
group :development, :test do
  gem 'dotenv-rails'
end

2) And run Dotenv stuff only if the environment matches your Gemfile group:

# config/application.rb
Bundler.require(*Rails.groups)
if ['development', 'test'].include? ENV['RAILS_ENV']
  Dotenv::Railtie.load
end
like image 180
dhilt Avatar answered Oct 20 '22 04:10

dhilt