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

nenv

v1.0.0

Published

Node development environment manager

Downloads

10

Readme

Table of Contents

Node Environment (NODE_ENV)

.

Utility for mananging node development environments.

Install

npm i nenv --save

Usage

var env = require('nenv')();
if(!env.defined) {
  // do something when no environment was specified
  // maybe: env.set(env.DEVEL) or whichever default you want
}else if(!env.valid) {
  // do something when the specified environment is invalid
  // and the debug flag is not set
}else if(env.test && !env.debug) {
  // do something for test environment
}

API

function nenv([environments, get, set])
  • environments: Array or object of custom environments, if not specified the defaults are used.
  • get: A custom function for getting the environment value (optional).
  • set: A custom function for setting the environment value (optional).
  • dbg: A string for the debug flag environment variable, default is DEBUG.

The returned query object maintains a special debug property which is a boolean indicating whether the DEBUG environment variable has been set, if this conflicts with an environment name either change the environments or dbg options.

The query object is maintained in a cache and will be returned on subsequenct calls which is typically desirable, if you need a fresh query delete nenv.cache.

env([value])

Determines if an environment value is valid. Returns false if the supplied value is invalid or the string key for the environment if the value is a known environment alias.

If no value is supplied then env.value is used which allows testing whether the default value is valid by calling with no arguments.

env.value

The value returned from get() when nenv() was called, the initial environment value.

env.valid

Boolean that determines whether env.value is a recognised environment.

env.defined

Determines whether an initial value (env.value) was defined.

env.get()

Get the current value of the environment, the default implementation returns process.env.NODE_ENV.

env.set(val)

Set the current value of the environment, the default implementation returns false if the supplied value is not a known environment alias otherwise a function that may be called to revert to the previous value.

env.keys

Array of environment keys.

env.map

Map of environment keys to arrays of string aliases for the environment.

env.jsonify()

Return an object suitable for passing to JSON.stringify.

nenv.defaults

Default values to use.

['test', 'devel', 'stage', 'production'];

nenv.cache

A cache created the first time nenv() was invoked. Typically you would always want to share the same environment query:

var nenv = require('nenv')
  , env = nenv()
  , newenv = nenv()
  // bypass cache and get a new query function using defaults
  , altenv = nenv(true);
console.log(env === newenv);
console.log(env === altenv);

You can bypass the cached instance by passing arguments to nenv() or alternatively you could delete nenv.cache to force a new query to be created.

nenv.get

Default get function.

nenv.set

Default set function.

Environments

Pass an object or array to define your available environments. Passing an object allows specifying multiple keys as aliases for the environment, useful to alias shortcuts for longer environment identifiers.

var nenv = require('nenv');
console.dir(nenv(['test', 'dev', 'stage']));
console.dir(nenv({production: ['production', 'pro'], dev: 'dev', test: 'test'}));

Getter

Use a fallback value by supplying a get function:

var nenv = require('nenv');
function fallback() {
  return process.env.NODE_ENV || this.PRODUCTION;
}
var env = nenv(fallback);
console.dir(env);

Or apply override logic to prefer another variable:

var nenv = require('nenv');
function override() {
  return process.env.ENV || process.env.NODE_ENV;
}
var env = nenv(override);
console.dir(env);

Example

See defaults.js.

var env = require('./')()
  , str = JSON.stringify(env.jsonify(), undefined, 2);
process.stdout.write(str);

Executed with NODE_ENV=devel, yields:

{
  "TEST": "test",
  "DEVEL": "devel",
  "STAGE": "stage",
  "PRODUCTION": "production",
  "debug": false,
  "value": "devel",
  "current": "devel",
  "valid": true,
  "defined": true,
  "map": {
    "test": [
      "test"
    ],
    "devel": [
      "devel"
    ],
    "stage": [
      "stage"
    ],
    "production": [
      "production"
    ]
  },
  "keys": [
    "test",
    "devel",
    "stage",
    "production"
  ],
  "test": false,
  "devel": true,
  "stage": false,
  "production": false
}

Developer

Test

Run the test specifications:

npm test

Cover

Run the test specifications and generate code coverage:

npm run cover

Lint

Run the source tree through jshint and jscs:

npm run lint

Readme

Generate the project readme file (requires mdp):

npm run readme

License

Everything is MIT. Read the license if you feel inclined.

Generated by mdp(1).