Demo environment variables and the dotenv package dotenv
- 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
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.
- Install it as a dependency
npm install dotenv --save
- 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>
- Add an entry for
.env
to.gitignore
file
.env
# Example of other ignored files and folders
node_modules
logs
*.log
- 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'});
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
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