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

deepenv

v1.2.0

Published

Takes environment variables and converts into a deeply nested configuration object based on a customisable delimiter (__) to represent a level of nesting in the config json (.)

Downloads

2

Readme

deepenv

Generates a deeply nested configuration object suitable for backend apps, using environment variables.

NPM version CircleCI

Best practice demands that configuration that changes across environments should be contained in the environment rather than in code. This helps with security, managing environments, portability and open-source development.

Unfortunately environment variables are flat string key-value pairs, whereas most apps organise their configuration in deeply nested objects. Environment variable names cannot include a period to indicate nesting.

This means backend developers have to take environment variables such as

MYAPP_MYDATASTORE_CONNECTION_RETRY_INTERVAL=5s

are manually wire them into the configuration object, EG:

const config = 
{
    mydatastore : {
        encoding : 'utf8'
        connection : {
            retry_interval : process.env['MYAPP_MYDATASTORE_CONNECTION_RETRY_INTERVAL']
        }
    },
    myotherconfig : {}
}

deepenv.js automates this process, saving time and enforcing consistency between environment variable names and configuration.

quick start (2 mins)

Install deepenv to a project directory that has been initialised with npm (npm init):

npm i deepenv

Create your application, generating the config using deepenv :

// file: myapp.js
const config = require('deepenv').deepenv() // or ES6: import 'deepenv'; const config = deepenv();
console.log(config)

Run your application using a double-underscored environment variable:

DEEPENV_A__B__C=4 node myapp.js

Observe the generated config

{ a: { b: { c: 4 } } }

deeper demo

Many configuration parameters need not be specified by the environment. These are the first argument to deepenv, so they can be merged with those that are specified by the environment.

The second argument is an object containing options, such as a custom prefix for environment variables that are used by deepenv.

With a double-underscored environment variable:

MYAPP_MYDATASTORE__CONNECTION__RETRY_INTERVAL=5s

And deepenv:

const config = require('deepenv').deepenv(
{   // pre-existing configuration, not mutated by deepenv
    mydatastore : {
        encoding : 'utf8'
    },
    myotherconfig : {}
}, 
{   // deepenv options
    custom_prefix 'MYAPP_'
})

deeply nested config is generated from the environment:

{
    mydatastore : {
        encoding : 'utf8'
        connection : {
            retry_interval : '5s'
        }
    },
    myotherconfig : {}
}

options

custom_prefix : override the default prefix 'DEEPENV_' with one based on the name of your app.

custom_nesting_delimiter : override the default nesting delimiter '__' with something else. Note that environment variable names must consist solely of uppercase letters, digits, and the '_'.

external dependencies

the only external dependency is lodash.set method, not the whole lodash library.

review of similar projects

env2object

allows for deep nesting to be defined in .env files, but does not allow for deep nesting based of generic environment variables given in other way, because dots aren't allowed in environment variable names: https://github.com/doanthuanthanh88/env2object

node-env2object

does allow for environment variable names to specify a nested object, however does not include adequate testing and customisation features: https://github.com/tombburnell/node-env2object

dotenv

popular project that reads from files into the environment, but doesn't help with the next step of going from environment variables into code config, for which manual wiring is still required. https://github.com/motdotla/dotenv