Wondering what’s next for npm?Check out our public roadmap! »

    lookenv

    1.0.1 • Public • Published

    lookenv

    lookenv

    Set rules for the environment variables in your project.

    NPM Version Build Status Downloads Stats

    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:

    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"
    }

    With dotenv

    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.

    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.

    {
      "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
      }
    }

    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 = require('lookenv')
     
    lookenv.validate()
      .then(() => {
        // ... your app goes here, basically...
      })
      .catch(error => {
        console.error(error)
        process.exit(1)
      })

    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

    1. Fork it (https://github.com/RodrigoEspinosa/lookenv/fork)
    2. Create your feature branch (git checkout -b feature/foo-bar)
    3. Commit your changes (git commit -am 'Add some foo and bar')
    4. Push to the branch (git push origin feature/foo-bar)
    5. Create a new Pull Request

    Keywords

    none

    Install

    npm i lookenv

    DownloadsWeekly Downloads

    342

    Version

    1.0.1

    License

    MIT

    Unpacked Size

    18 kB

    Total Files

    20

    Last publish

    Collaborators

    • avatar