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

envlist

v1.2.0

Published

envlist is a micro-module (without dependency) for resolving the type of runtime environment between your application convention and another convention (like NODE_ENV).

Downloads

4

Readme

envlist

Micro-module (without dependency) for resolving the type of runtime environment between your application convention and another convention (like NODE_ENV).

Why?

The problem

Like the debug of Express and some Express middlewares, few modules of Node.js require to put NODE_ENV to production or development for changing the behavior of execution.

In the real world, it is common to use several environment names like local, staging, testing. Also it is common to use shortcuts and several environment names like prod, dev, stage, test, local.

The problem is that in defining NODE_ENV to local (or another alias of a development env), we do not get the full functionality of debugging of some modules.

Also in defining NODE_ENV to prod, we do not get the optimizations made to the production environment.

The solution

A simple and useful solution is to map your application environment to a conventional NODE_ENV environment. Then consolidate all :bulb:

Install

npm install --save envlist

or

yarn add envlist

Usage

By default envlist has preconfigured environments (of course you can replace it).

let EnvList = require('envlist');
let envList = new EnvList();

List all types of environments

console.log(envList.envs);

Output:

  local: {
    APP_ENV: 'local',
    NODE_ENV: 'development'
  },
  dev: {
    APP_ENV: 'dev',
    NODE_ENV: 'development'
  },
  stage: {
    APP_ENV: 'stage',
    NODE_ENV: 'production'
  },
  test: {
    APP_ENV: 'test',
    NODE_ENV: 'development'
  },
  prod: {
    APP_ENV: 'prod',
    NODE_ENV: 'production'
  }
}

Customize a specific environment

You can customize each environment. Example by default the value of envList.envs.test.NODE_ENV is development. If you want to change this value:

envList.envs.test.NODE_ENV = 'production';

envList.envs it's just an object (see above).

Use your own set of environments

envList.envs = {
  development: {
    APP_ENV: 'development',
    NODE_ENV: 'development'
  },
  production: {
    APP_ENV: 'production',
    NODE_ENV: 'production'
  },
  staging: {
    APP_ENV: 'staging',
    NODE_ENV: 'production'
  },
  test: {
    APP_ENV: 'test',
    NODE_ENV: 'production'
  },
  demoProd: {
    APP_ENV: 'demoProd',
    NODE_ENV: 'production'
  },
  demoDev: {
    APP_ENV: 'demoDev',
    NODE_ENV: 'development'
  }
};

Get a specific environment name

// dev
console.log(envList.envs.dev.APP_ENV);
console.log(envList.get('dev').APP_ENV);

// development
console.log(envList.envs.dev.NODE_ENV);
console.log(envList.get('dev').NODE_ENV);

// prod
console.log(envList.envs.prod.APP_ENV);
console.log(envList.get('prod').APP_ENV);

// production
console.log(envList.envs.prod.NODE_ENV);
console.log(envList.get('prod').NODE_ENV);

Get the current environment

Considers that the process was launched with the command: node APP_ENV=dev app.js

// dev
envList.env;

// dev
envList.APP_ENV;

// development
envList.NODE_ENV;

/*
  {
    APP_ENV: 'dev',
    NODE_ENV: 'development'
  }
*/
envList.getCurrent();

Ensure the current environment

It may be useful to ensure that the current environment is good and change it if it's not the desired one.

Example, ensure the dev environment:

envList.ensure('dev');

In this example, if the current environment is not equal to dev, this method change and consolidate the current environment.

Does nothing if the current environment is equal to dev.

Use your own resolver

Considers that you have installed Gulp and gulp-util and you want to support env.type:

let gutil = require('gulp-util');

envList.resolveAppEnv = function() {
  envList.env = process.env.APP_ENV || process.env.NODE_ENV || gutil.env.type;

  if(envList.env && envList.has(envList.env)) {
    return this;
  }

  throw new ReferenceError('Environment not found.');
}

Use your own consolidator

Considers that you have installed Gulp and gulp-util and you want consolidate also env.type:

let gutil = require('gulp-util');

envList.consolidate = function() {
  let current;

  if(!envList.env) {
    envList.resolveAppEnv();
  }

  if(process && process.env) {
    current = envList.getCurrent();

    process.env.APP_ENV = current.APP_ENV;
    process.env.NODE_ENV = current.NODE_ENV;
    gutil.env.type = current.NODE_ENV;
  }

  return envList;
}

If you use a framework that handle the environment based on NODE_ENV value (like Express.js), consolidate the environment before loading Express.js.

Unit tests

envlist is unit tested with Mocha and Unit.js.

Run the tests

cd node_modules/envlist && npm install && npm test

LICENSE

MIT (c) 2016, Nicolas Tallefourtane.

Author

| Nicolas Tallefourtane - Nicolab.net | |---| | Nicolas Talle | | Make a donation via Paypal |