33

I know there are the same/similar questions on stack overflow and I have read the documentation too-I just still don't understand ANYTHING- how to set those variables and WHERE!! to set them.

My env/production.js file:

module.exports = {
    "DATABASE_URI": process.env.DATABASE_URI,
    "SESSION_SECRET": process.env.SESSION_SECRET,
    "TWITTER": {
       "consumerKey": process.env.TWITTER_CONSUMER_KEY,
       "consumerSecret": process.env.TWITTER_CONSUMER_SECRET,
       "callbackUrl": process.env.TWITTER_CALLBACK
    },
    "FACEBOOK": {
        "clientID": process.env.FACEBOOK_APP_ID,
        "clientSecret": process.env.FACEBOOK_CLIENT_SECRET,
        "callbackURL": process.env.FACEBOOK_CALLBACK_URL
    },
    "GOOGLE": {
        "clientID": process.env.GOOGLE_CLIENT_ID,
        "clientSecret": process.env.GOOGLE_CLIENT_SECRET,
        "callbackURL": process.env.GOOGLE_CALLBACK_URL
    },
    "LOGGING": true
};

In my env/development.js file I set the variables (linked to my PostgreSQL/localhost/xxx). Do I need to set them in Heroku for all (Google, Facebook etc.) or just for the database, since I had to create one with Heroku? Do I leave the link to my local database in my development file and link to the Heroku database separately?

I don't even know if I am supposed to do it from my command line?! In the documentation it says: Heroku config:get CONFIG-VAR-NAME -s >> .env so would it be Heroku config:get CONFIG-NAME OF MY HEROKU DATABASE -s >> .env ?

I'm deploying for the first time and so confused! Help :)

BinaryButterfly
  • 18,137
  • 13
  • 50
  • 91
javascript2016
  • 973
  • 3
  • 17
  • 41
  • It can be done in UI, In heroku dashboard for the app, go to settings and set up you environmental variables – dark_ruby Oct 19 '16 at 00:19
  • Okay found it in settings! Thank you!! Also, for facebook and google-would I set the callbackUrl then? Also, as I understood, I am also suppose to set the NODE_ENV=production -could you explain how?Thank you! – javascript2016 Oct 19 '16 at 01:26
  • see my answer below – dark_ruby Oct 19 '16 at 18:01

6 Answers6

53

According to documentation you could use heroku CLI

heroku config:set DATABASE_URI=database_uri_here --app your-app-name
heroku config:set SESSION_SECRET=session_secret --app your-app-name

or you could use UI https://dashboard-classic.heroku.com/apps/{your-app-name}/settings and provide the same variables via web interface, as I mentioned in the above comment

NODE_ENV=production is not treated specially by heroku, so you do need to provide it as well as any other env variable

BinaryButterfly
  • 18,137
  • 13
  • 50
  • 91
dark_ruby
  • 7,646
  • 7
  • 32
  • 57
35
heroku config:set $(cat .env | sed '/^$/d; /#[[:print:]]*$/d')

set your config from .env file

Tay
  • 371
  • 3
  • 6
5

Especially database connection variables will not work through Heroku env variables. I would suggest using dotenv and set variables to the file by installing Heroku CLI and then use bash:

heroku login
heroku run bash -a app_name

create .env file if it is not there and add values to it

touch .env
echo "ENV_VAR=value" >> .env

confirm the entry in a file

cat .env
Avtar Nanrey
  • 133
  • 3
  • 9
2

Heroku now automatically sets the DATABASE_URL environment variable when you add the PostgreSQL add-on

This variable can then be seen just next to the other environment variables, possibly manually defined, under:

  • Settings
  • Config Vars

That added variable already contains secret authentication information, so you should just use it as is.

How to get it working with Sequelize

I just managed a working solution with Sequelize as mentioned at: Deploy FeathersJS App on Heroku with Sequelize, you basically just need to do:

sequelize = new Sequelize(process.env.DATABASE_URL, {
    dialect: 'postgres',
    protocol: 'postgres',
    dialectOptions: {
        ssl: {
            require: true,
            rejectUnauthorized: false
        }
    }
});

as mentioned at: Can't connect to heroku postgresql database from local node app with sequelize

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
1

Loggin in Heroku CLI:

heroku login

list all apps:

heroku apps

Massive export of environment variables

cat .env | tr '\n' ' ' | xargs heroku config:set -a your_app
Marcelo
  • 367
  • 3
  • 7
1

Another option:

$ heroku login

then edit all config variables:

$ VISUAL="emacs" heroku config:edit -a <app_name>

This opens emacs where you can copy/paste the entire .env content. Then save and quit (Ctrl+x Ctrl+s, Ctrl+x Ctrl+c).

milan
  • 11,872
  • 3
  • 42
  • 49