NameError: uninitialized Dotenv constant when clicking on rails heroku app
I am using dotenv to store environment variables and since I included it in the gemfile I cannot push it to hero. I am 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 include dotenv in my gemfile: -
gem 'dotenv-rails', :require => 'dotenv/rails-now'
I tried to add the following to my application.rb file: -
Bundler.require(*Rails.groups)
Dotenv::Railtie.load
HOSTNAME = ENV['HOSTNAME']
still not working.
I don't know if those two lines that say "unterminited quoteed string" might have some unrelated issue leading to dotenv not loading. I went through it and checked the heroku config to see if there is something in the variables, but they all look fine. I was able to push before adding dotenv to the gemfile.
I tried to run the package installation, restart the server, remove the gemfile.lock and start the package installation and I looked at this issue here and tried the solutions suggested in Can't push to Heroku because uninitialized permanent DOTENV error
Still no luck.
PS - I am trying to implement recaptcha and it is recommended to use dotenv to store site_key and secret_key for recaptcha as env vars. So I am trying to get this to work.
source to share
I faced the same problem and was able to solve it with the following.
1) Add dotenv-rails
in Gemfile
only for certain environments:
# Gemfile
group :development, :test do
gem 'dotenv-rails'
end
2) And run the file Dotenv
only if the environment matches your group Gemfile
:
# config/application.rb
Bundler.require(*Rails.groups)
if ['development', 'test'].include? ENV['RAILS_ENV']
Dotenv::Railtie.load
end
source to share