Socket
Book a DemoInstallSign in
Socket

envy

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

envy

Load .env files and environment variables

latest
Source
npmnpm
Version
3.0.0
Version published
Maintainers
1
Created
Source

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.

Contents

Why?

You want to keep your secrets safe, right? envy loads your app's configuration values from a .env file and prevents mistakes, such as accidentally committing the .env file to your repository, or leaving .env with unsafe permissions.

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

const envy = require('envy');
const env = envy();
console.log(env);
// {
//     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 must add this file to a local .gitignore to prevent leaking secrets and envy will check that you did this correctly.

# .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

  • meow - Make command line apps

Contributing

See our contributing guidelines for more details.

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

License

MPL-2.0 © Seth Holladay

Go make something, dang it.

Keywords

env

FAQs

Package last updated on 13 Aug 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts