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

jenvcfg

v1.1.3

Published

Loads environment variables from a JSON file with comments in constant case

Downloads

9

Readme

jenvcfg

Jenvcfg combines the concepts of using a .env and a JSON file for configuration. The config file is a JSON file (which allows comments) and is loaded into the user environment (process.env) like a .env file. The JSON object gets flattened, then each key is converted into constant case and asssigned to the user environment (process.env).

Install

To install jenvcfg execute the following command in your terminal

    npm install jenvcfg

Usage

Example jenvcfg.json file:

{
    // Hi, im a comment and im allowed
    "port" : 8080,
    "general" : {
        "name" : "example",
        /**
         * Im also allowed wuhuu
         */
        "information" : {
            "title" : "best config",
            "text" : "this is how it works"
        }
    }
}

Add the desired lines of code at the beginning of the application to load the configuration file:

    // Require the package
    const jenvcfg = require('jenvcfg'):

    // Load the default config file - jenvcfg.json
    jenvcfg.load();

    // Load a specified config file
    jenvcfg.load('jenvcfg.json');

    // Load the config file depending on the current environment [NODE_ENV].jenvcfg.json
    jenvcfg.loadByEnv();

The values of the example config file can be accessed via:

// port
process.env.PORT // 8080

// general.name
process.env.GENERAL_NAME // example

// general.information.title
process.env.GENERAL_INFORMATION_TITLE // best config

// general.information.text
process.env.GENERAL_INFORMATION_TEXT // this is how it works

Documenation

load

load will load either the optional specified file or default to "jenvcfg.json". Due to the nature of the process.env object all values from the config will be implicitly converted to string (See process.env for further information). It will return an object containing all new set environment variables.

    {
        PORT : "8080",
        GENERAL_NAME : "example",
        GENERAL_INFORMATION_TITLE : "best config",
        GENERAL_INFORMATION_TEXT : "this is how it works"
    }

In case of an error an object containing an error key with the error will be returned.

    {
        error : Error(...)
    }

loadByEnv

loadByEnv will call the load function but will add the NODE_ENV as prefix to the "jenvcfg.json" so that the file "NODE_ENV.jenvcfg.json" will be loaded. If NODE_ENV if not set it will default to "development".