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

envy

v2.0.0

Published

Load .env files and environment variables

Downloads

300

Readme

envy

Load .env files and environment variables

Build status for Envy

Secure and friendly alternative to dotenv, using functional programming. With strong tests to prove its safety.

Follows the Twelve Factor App methodology.

Looking for the old envy that reads JSON? See eliOcs/node-envy.

Contents

Why?

  • Safest environment loader around.
  • Validates file permissions are secure.
  • Validates required env vars are defined.
  • No side effects, does not modify process.env.
  • Returns property names in camelCase.
  • Asserts that .env is ignored in git.

Have you spent a day rotating passwords because a developer accidentally pushed them to the repository? Yeah, that actually happens and it can cause chaos at companies. It doesn't matter if you delete the commit. Once it's out there, game over. People who are authorized to read the repository may not be authorized to have those credentials, including third party tools and services. If the repository is public, search engines may have crawled it. Consider everything to be compromised.

The envy module helps you prevent that situation by providing a convenient mechanism for everyone to store credentials and other config locally and validate that it is correct without commiting them to the repository. It verifies that all relevant files have secure permissions and that the secrets file is explicitly ignored so that it cannot accidentally be commited.

Install

npm install envy --save

Usage

Get it into your program.

const envy = require('envy');

Load the environment variables.

const env = envy();
// {
//     foo : 'bar'
// }

A .env.example file is used as a template to determine which environment variables are required by your application. This file should be commited to your repository.

# .env.example
FOO=

A .env file is used to provide the actual environment values. You should add this file to a local .gitignore.

# .env
FOO=bar

Environment files look just like normal shell files (e.g. .bashrc and friends). Values may be optionally wrapped in ' single quotes. This is recommended for shell compatibility, in case the value contains whitespace.

# This is a comment.
FOO=bar
SENTENCE='Hi, there!'

After assembling the environment from .env and process.env, it is filtered by a union of the properties in .env and .env.example. In other words, variables defined in either file will be returned and variables not defined in either file will be ignored. This is done to prevent surprising behavior, since process.env often contains a wide variety of variables, some of which are implicitly set without the user's direct knowledge. It is better to be explicit about the variables you use, which improves safety and debugging.

API

envy(filepath)

Returns an object with environment variables derived from process.env and the contents at filepath. If a variable is defined in both places, process.env takes precedence so that users can easily override values on the command line. If all required variables are present in process.env, then the .env file need not exist. All property names are returned in camelcase.

filepath

Type: string Default: .env

Path to the file where environment variables are kept. Must be hidden (start with a .). Will also be used to compute the path of the example file (by appending .example).

Tips

Mixing with command line options

Make a CLI with meow and use joi to further validate and parse the returned values.

// cli.js
const meow = require('meow');
const envy = require('envy');
const joi = require('joi');

const cli = meow();

const input = Object.assign({}, envy(), cli.flags);
const config = joi.attempt(input, {
    port : joi.number().positive().integer().min(0).max(65535)
});
// value.port has been parsed as a number
console.log('port:', config.port);
# .env.example
PORT=
# .env
PORT=1000
$ node cli.js
port: 1000
$ PORT=2000 node cli.js
port: 2000
$ node cli.js --port=3000
port: 3000
$ PORT=2000 node cli.js --port=3000
port: 3000

Modifying process.env

We recommend not using process.env directly. But let's say you want to because a dependency expects you to set NODE_ENV and you want to define it in .env. No problem!

You should first ask the project to accept options as input so you don't need this.

const envy = require('envy');
const decamelize = require('decamelize');

const override = Object.entries(envy()).reduce((result, [key, val]) => {
    result[decamelize(key).toUpperCase()] = val;
    return result;
}, {});
Object.assign(process.env, override);

FAQ

Why not dotenv or dotenv-safe?

I see this as a successor to those projects. But envy would not exist without them. dotenv brought .env files mainstream in Node.js projects. dotenv-safe improved on it by introducing the .env.example file. Now, envy takes this even further by securing your files and returning an object with camelcase keys rather than modifying process.env.

Resources

Related

  • meow - Make command line apps

Contributing

See our contributing guidelines for more details.

  1. Fork it.
  2. Make a feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request.

License

MPL-2.0 © Seth Holladay

Go make something, dang it.