10

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.

Ragav Y
  • 1,662
  • 1
  • 18
  • 32

2 Answers2

11

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
dhilt
  • 18,707
  • 8
  • 70
  • 85
  • would be nice if this was in the documentation – Carl Jan 25 '20 at 00:09
  • 1
    Thanks for this, @dhilt. For other folks' reference in future, I surrounded the Dotenv call in config/initializers/dotenv.rb with `unless ENV['RAILS_ENV'] == 'production'...end` (which is cleaner than checking for two environments IMO). – JohnP Feb 27 '20 at 12:51
  • actually, for the case when ENV is used in other GEMS the `dotenv-rails` can be included at the first line of the Gemfile `gem 'dotenv-rails', groups: [:development, :test]` – Semen Shekhovtsov Jun 28 '20 at 04:15
  • `Dotenv::Railtie.load unless Rails.env.production?` feels cleaner and more straight forward to me – SMAG Mar 08 '22 at 06:32
-4

Try adding the gem to the production group.

group :production do
  gem 'dotenv-rails'
end
William Holt
  • 549
  • 4
  • 14