Skip to content

CodeDemos/demo-dotenv

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

dotenv-demo

Demo environment variables and the dotenv package dotenv

Environment Variables

Setting Environment Variables

  • Single execution. The envvar exists for the life of the execution.
SECRET=abracadabra node server.js
  • Current session. The envvar exists for the life of the terminal session.
export SECRET=password
echo $SECRET
node server.js
  • Add to ~/.bash_profile or .bashrc
export SECRET=abracadabra

The dotenv package

For convenience you can use the dotenv package.

The dotenv module loads the .env file, parses it and adds the values to process.env which is global so it only needs to be required once, typically at the start of your app.

  1. Install it as a dependency
npm install dotenv --save
  1. Create a .env file and add values

Create a file named .env. And add the name=values.

Note: no space surrounds the =

SECRET=mypassword
DATABASE=postgres://<username>:<password>@stampy.db.elephantsql.com:5432/<username>
  1. Add an entry for .env to .gitignore file
.env

# Example of other ignored files and folders
node_modules
logs
*.log
  1. Require and config dotenv

Near the top of server.js or config.js

require('dotenv').config();

Or you can pass a path to load your file from a custom path

require('dotenv').config({path: '/path/to/your/env/file'});

Using Environment Varaibles on Travis

There are 2 ways to add envvars on Travis

  • Define variables in the repository settings.
    • Go to: PROJECT > Settings > Environment Variables
  • Or add encrypted variables to your travis.yml like so: travis encrypt MY_SECRET_ENV=super_secret --add env.matrix

Defining-encrypted-variables-in-Travis.yml

Using Environment Varaibles On Heroku

And there are 2 ways to add envvars in Heroku.

  • Run heroku config:set SECRET=password
  • Or in: YOUR-APP > Settings > "Reveal Config Vars" button

For more information check out the documentation: config-vars

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published