lookenv
Set rules for the environment variables in your project.
lookenv
can check if all the variables you need are present before starting your app. It also can set defaults for those variables that are not present. Works fine with dotenv
or dotenv-safe
.
Installation
npm install lookenv --save # Or with yarn yarn add lookenv
Usage example
Create a lookenv.config.js
file, or .lookenvrc
that exposes a JSON like the following:
moduleexports = MY_ENV_VAR: required: true MY_SECOND_ENV_VAR: default: 'testing'
Then, add lookenv
to the package.json
start script, before the app starts but after dotenv (if you are using it!).
You can also specify a path to the config file, or the directory where the config file by passing --path
or -p
.
dotenv
With You can pass a --dotenv
(or -d
for short) to the cli to load dotenv
before
validating the env vars.
"start": "lookenv --dotenv -- node index.js"
You can optionally pass the location of your .env
in the --dotenv
option,
like lookenv --dotenv=/path/to/custom/env -- node index.js
.
Joi
With Joi, the object schema description language and validator for JavaScript objects.
Lookenv recognizes and supports Joi schemas from the config files. In order to do so, please remember to install (npm install --save joi
) in your project. And then, export the Joi schema in your lookenv.config.js
file.
const Joi = ; moduleexports = Joiobject
This means that you can use the entire Joi Schema API to validate your env vars.
Using it just for setting defaults
Everything would be the same, but you can use the simplified lookenv.config.js
(or .lookenvrc
) json that matches every key with a default.
You can also combine them!
API
lookenv.config({ path })
This method will only call the config and return the set of rules, it won't do any validation.
lookenv.validate({ path, context })
This method will get the config for the lookenv.config.js
(or .lookenvrc
) from the current working directory (using process.cwd()
), unless you specify a path
to the config file in question.
After that, it will validate the context
(that is process.env
as default) and apply all the defaults.
If there is a required variable that isn't present, it will throw an error specifying the missing variables.
Programmatic use
const lookenv = lookenv
Remember that lookenv.validate
is async.
Development setup
This project use ava
to run tests. Just fork it.
npm test # Or with yarn yarn test
Release History
See CHANGELOG.md
Meta
REC – @reciam – yo@rec.cool
Distributed under the MIT license. See LICENSE
for more information.
https://github.com/RodrigoEspinosa/lookenv
Credits of the logo goes to @guillecura.
Contributing
- Fork it (https://github.com/RodrigoEspinosa/lookenv/fork)
- Create your feature branch (
git checkout -b feature/foo-bar
) - Commit your changes (
git commit -am 'Add some foo and bar'
) - Push to the branch (
git push origin feature/foo-bar
) - Create a new Pull Request