Identical to dotenv
, but ensures that
all necessary environment variables are defined after reading from .env
.
These needed variables are read from .env.example
, which should be commited
along with your project.
npm install --save dotenv-safe
# .env, private
SECRET=topsecret
# .env.example, commited to repo
SECRET=
TOKEN=
KEY=
require('dotenv-safe').load();
Since the provided .env
file does not contain all the variables defined in
.env.example
, an exception is thrown:
Error: Missing environment variables: TOKEN, KEY
Requiring and loading is identical:
require('dotenv-safe').load();
This will load environment variables from .env
as usual, but will also read
any variables defined in .env.example
. If any variables are missing from
.env
, an exception listing them will be thrown. Otherwise, returns true
.
dotenv-safe
compares the actual environment after loading .env
with the
example file, so it will work correctly if environment variables are missing
in the .env
but provided through other means such as a shell script.
You can use .env.example
or provide a different filename:
require('dotenv-safe').load({sample: './.my-env-sample-filename'});
Same options and methods supported by dotenv
.
I regularly use apps that depend on .env
files but don't validate if all
the necessary variables have been defined correctly.
Instead of having to document and validate this manually, I prefer to commit
a sample .env
file (no values, key names only) which can be used as a
reference.