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
.
npm install lookenv --save
# Or with yarn
yarn add lookenv
Create a lookenv.config.js
file, or .lookenvrc
that exposes a JSON like the following:
module.exports = {
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!).
{
"start": "lookenv -- node index.js"
}
You can also specify a path to the config file, or the directory where the config file by passing --path
or -p
.
{
"start": "lookenv --path=lookenv.config.js -- node index.js"
}
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
.
With Joi
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 = require('joi');
module.exports = Joi.object().keys({
A_NUMBER: Joi.number().required(),
A_STRING: Joi.string().required(),
AN_OBJECT: Joi.string().required(),
A_PORT: Joi.number()
.positive()
.default(3000),
A_NUMBER_WITH_DEFAULTS: Joi.number().default(7),
A_STRING_WITH_DEFAULTS: Joi.string().default('seven')
})
This means that you can use the entire Joi Schema API to validate your env vars.
Everything would be the same, but you can use the simplified lookenv.config.js
(or .lookenvrc
) json that matches every key with a default.
{
"MY_ENV_VAR": "my-default",
"MY_2ND_ENV_VAR": "other-default"
}
You can also combine them!
{
"MY_ENV_VAR": "my-default",
"MY_2ND_ENV_VAR": {
"required": true
}
}
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.
const lookenv = require('lookenv')
lookenv.validate()
.then(() => {
// ... your app goes here, basically...
})
.catch(error => {
console.error(error)
process.exit(1)
})
Remember that lookenv.validate
is async.
This project use ava
to run tests. Just fork it.
npm test
# Or with yarn
yarn test
See CHANGELOG.md
REC – @reciam – [email protected]
Distributed under the MIT license. See LICENSE
for more information.
https://github.com/RodrigoEspinosa/lookenv
Credits of the logo goes to @guillecura.
- 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