npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

lookenv

v1.0.1

Published

Set rules for the environment variables in your project. Works great with dotenv

Downloads

2,531

Readme

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[email protected]

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