Skip to content

Environment Variables

Polina Soshnin edited this page Jan 15, 2016 · 1 revision

Environment variables in Development

Use Node Foreman to keep track of our environment variables! 🎈 🎈 🔮

A few notes:

  • Install globally to your machine using npm install -g foreman
  • Create a file called .env in your root folder. It's gitignored so it won't be sent with your PR.
  • Store any variables in your .env that you want 'process.env' to know about.
  • Delete your config.js, you won't be needing it anymore.

For example, since our project currently uses:

var consumer_key = process.env.CONSUMER_KEY;
var consumer_secret = process.env.CONSUMER_SECRET;
var access_token_key = process.env.ACCESS_TOKEN_KEY;
var access_token_secret = process.env.ACCESS_TOKEN_SECRET;

In your .env file you want to have the following:

CONSUMER_KEY='asdfjlaksdfj'
CONSUMER_SECRET='aslkdfjalskdjf'
ACCESS_TOKEN_KEY='alskdfjalskdjf'
ACCESS_TOKEN_SECRET='asldkfjalksdjf'

Node Foreman needs configuration in both Procfile and package.json to load vars. Since our Procfile specifies nodemon make sure you have nodemon installed locally. Our package.json is set to run nf start when you run node start, so to start the server/load the vars simply do:

$ npm start

> trendr@1.0.0 start /Users/psoshnin/Desktop/makersquare/greenfield/trendr-PERSONAL/trendr
> nf start

[OKAY] Loaded ENV .env File as KEY=VALUE Format
[OKAY] Trimming display Output to 96 Columns
3:22:02 PM web.1 |  [nodemon] 1.8.1
3:22:02 PM web.1 |  [nodemon] to restart at any time, enter `rs`
3:22:02 PM web.1 |  [nodemon] watching: *.*
3:22:02 PM web.1 |  [nodemon] starting `node app.js`
3:22:02 PM web.1 |  Listening on: 5000

Make sure to also use localhost:5000 now :)

Environment variables in Production

Setting Heroku variables:

$ heroku config:set CONSUMER_KEY=sldfjslkdjfs
$ heroku config:set CONSUMER_SECRET=asdfasdfasdfa
$ heroku config:set ACCESS_TOKEN_KEY=woeiruwoeiuroweir
$ heroku config:set ACCESS_TOKEN_SECRET=lsknvolkajsnv

Viewing current Heroku variables:

$ heroku config
=== mks-trendr Config Vars
ACCESS_TOKEN_KEY:    sldfjslkdjfs
ACCESS_TOKEN_SECRET: sldfjslkdjfs
CONSUMER_KEY:        sldfjslkdjfs
CONSUMER_SECRET:     sldfjslkdjfs

Accessing Heroku variables in Node:

process.env.CONSUMER_KEY 
process.env.CONSUMER_SECRET
process.env.ACCESS_TOKEN_KEY
process.env.ACCESS_TOKEN_SECRET

Resources

Clone this wiki locally