Search code examples
node.jsherokuenvironment-variables

Node JS environment variables and Heroku deployment


I have a project using the dotenv package to load my environment variables in my NodeJS application I use the following line

var dotenv = require('dotenv').load({ silent: true }); 

//Example of use
username: process.env.CONVERSATION_USERNAME

I am now planning to deploy this application on Heroku. However, for some obvious security reasons i don't want to commit my .env file.

I'm new to NodeJS and I would like to know if there is a way to say "If the .env file doesn't exists, load the environment variable from Heroku"

Thanks, Alexi


Solution

  • Do not commit your .env to git (i.e. it should be in your .gitignore).

    You define env vars on Heroku either via your Heroku dashboard, or with heroku config:set, e.g.

    heroku config:set CONVERSATION_USERNAME=Alex
    

    See here for more information.