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

envconfig

v1.0.2

Published

Environment based configuration with automatic checking

Downloads

91

Readme

envconfig

Store configuration in environment variables with automatic verification. If a test/stage/prod environment has not been configured correctly, the app can be written to fail immediately telling the installer that they need to make sure the environment is valid.

In a locked down read-only environment, this avoids operations teams deploying an app but failing to create a directory that an application needs. It also prevents the app from being deployed to production without overriding a database connection string.

Inspired by http://12factor.net/config

Features:

  • Uses an environment variable if it is set.
  • Provides a default environment variable if one is not set.
  • Checks for required environment variables that have not been set
  • Checks for directories that don't exist
  • Will create directories if desired
  • Converts environment variables from string to appropriate type (number, string, array)

You can pass environment variables in via the command line or they can be set in a script. Here is an example of setting them via the command line:

NODE_ENV=test UPLOADS=./uploads node example

In the following example, if this is run for the first time, it will fail with an error stating that the path does not exist. It is assumed that environments other than development will be configured with NODE_ENV set to something other than 'development'.

example

var envconfig = require('envconfig');
var env = envconfig({
  NODE_ENV: 'development',
  UPLOADS: {
    required: false,  // If true, checkSync will throw an error if not set
    value: './uploads', // value to use if env not overridden
    isDirectory: true, // If true, checkSync will throw an error if doesn't exist
    description: 'This is the directory where uploaded files are stored.'
  },
  ARR: ['1','2','3'],
  MAX_SIZE_BYTES: 1024 // 1k
});

if (env.NODE_ENV !== 'development') {
  env.check()
    .fail(function (errors) {
      for (var i = 0; i < errors.length; i++) {
        console.error(errors[i].message);
      }
      process.exit(1);
    });
} else {
  // Create directories if they don't exist to make life easier
  env.createDirectories()
    .fail(function (err) {
      console.error(err);
      process.exit(1);
    });
}

console.log('Uploads path: ' + env.UPLOADS);
console.log('Max filesize (bytes): ' + env.MAX_SIZE_BYTES);
console.log('element 0 for ARR: ' + env.ARR[0]);