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

load-env

v1.3.6

Published

Load your environment configuration in a easy way and only one time.

Downloads

26

Readme

load-env

Load your environment configuration in a easy way and call in your code only one time and will be available through "process.env".

Build Status

V1.3

It's possible to reload the environment configuration by change the config file. Attention, process.env convert all values to string, must cast in case isn't a string, ex:

port number for your web server => 3000

console.log(process.env.WEB_PORT);// is going be '3000'
console.log(Number(process.env.WEB_PORT));// is going be 3000


a JSON object with some options => {port: 4000, address: 'localhost'}

console.log(process.env.NET_OPTIONS);
// is gonna be '{"port":4000,"address":"localhost"}'
// internally we are using JSON.stringify

console.log(JSON.parse(process.env.NET_OPTIONS));
// is gonna be { port: 4000, address: 'localhost' }

If wants to get the primitive value of the specified object can use process.loadenv.'key' and don't need to do any conversion.

 typeof process.loadenv.MONGODB_URL === 'string'
 typeof process.loadenv.APP_PORT === 'number'
 isJSON(process.loadenv.WINDOW_SIZE, true)

####API

var load = require('load-env') or require('load-env')()

load([environment], [config_path], [reload*])

*reload accepts a bool (default to false/null), if wants to reload the enviroment when the configuration file is updated then pass true.

from CLI: node my_app --env heroku

in your app just need to make the require and call load();

--env is mandatory in CLI

config folder must be in your application current directory or can use [path] to define the localization of your config files, and the files must have the exactly name as your environment (heroku.json will be env heroku).

Example:

  config/
      development.json
      heroku.json
      local.json

####JSON strutucture for config file

{"var_name": {"format": "", "value":""}}

format: value with placeholders
    %s - String
    %d - Number (both integer and float)
    %j - JSON
value: object with the values to replace the placeholders

Example

    {
      "MONGODB_URL": {
        "format": "mongodb://%s:%s@%s/%s?%s",
        "value" : {
          "user": "xpto",
          "pwd": "password",
          "host": "ec2-22-197-555-120.compute-100.amazonaws.com",
          "db": "lilidb",
          "extra": "numberOfRetries=10&retryMiliSeconds=10000"
        }
      },
      "APP_PORT": {
        "format": "%d",
        "value" : {
          "port": 4000
        }
      }
    }



   // now in your code

   require('load-env')(); // var load = require('load-env'); load();

   console.log(process.env.MONGODB_URL); // 'conn_str'
   console.log(process.env.APP_PORT); // '4000'
	
OR
   console.log(process.loadenv.MONGODB_URL); // 'conn_str'
   console.log(process.loadenv.APP_PORT); // 4000
   
   // CLI
   node my_app --env heroku
   
   

Example with JSON objects in the configuration

{
  "WINDOW_SIZE": {
    "format": "%j",
    "value": {
      "sizes": {
        "width": 1000,
        "height": 600,
        "min-width": 800,
        "min-height": 600
      }
    }
  }
}	  

// we are using %j to specify is a JSON

call process.loadenv.WINDOW_SIZE will return the the JSON already parsed

{ width: 1000, height: 600, 'min-width': 800, 'min-height': 600 }

call process.env.WINDOW_SIZE must use the JSON.parse