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

getenv

v1.0.0

Published

Get and typecast environment variables.

Downloads

3,962,366

Readme

getenv

Build Status

Helper to get and typecast environment variables. This is especially useful if you are building Twelve-Factor-Apps where all configuration is stored in the environment.

Installation

npm install getenv

TypeScript types are available from the @types/getenv module.

Usage

Set environment variables:

export HTTP_HOST="localhost"
export HTTP_PORT=8080
export HTTP_START=true
export AB_TEST_RATIO=0.5
export KEYWORDS="sports,business"
export PRIMES="2,3,5,7"

Get and use them:

const getenv = require('getenv');

const host = getenv('HTTP_HOST'); // same as getenv.string('HTTP_HOST');
const port = getenv.int('HTTP_PORT');
const start = getenv.bool('HTTP_START');

if (start === true) {
  // const server = http.createServer();
  // server.listen(port, host);
}

const abTestRatio = getenv.float('AB_TEST_RATIO');

if (Math.random() < abTestRatio) {
  // test A
} else {
  // test B
}

const keywords = getenv.array('KEYWORDS');
keywords.forEach(function(keyword) {
  // console.log(keyword);
});

const primes = getenv.array('PRIMES', 'int');
primes.forEach(function(prime) {
  // console.log(prime, typeof prime);
});

Methods

All methods accept a fallback value that will be returned if the requested environment variable is not set. If the fallback value is omitted and if the requested environment variable does not exist, an exception is thrown.

env(name, [fallback])

Alias for env.string(name, [fallback]).

env.string(name, [fallback])

Return as string.

env.int(name, [fallback])

Return as integer number.

env.float(name, [fallback])

Return as float number.

env.bool(name, [fallback])

Return as boolean. Only allows true/false as valid values.

env.boolish(name, [fallback])

Return as boolean. Allows true/false/1/0 as valid values.

env.array(name, [type], [fallback])

Split value of the environment variable at each comma and return the resulting array where each value has been typecast according to the type parameter. An array can be provided as fallback.

env.multi({spec})

Return a list of environment variables based on a spec:

const config = getenv.multi({
  foo: 'FOO', // throws if FOO doesn't exist
  bar: ['BAR', 'defaultval'], // set a default value
  baz: ['BAZ', 'defaultval', 'string'], // parse into type
  quux: ['QUUX', undefined, 'int'], // parse & throw
});

env.url(name, [fallback])

Return a parsed URL as per Node's require("url").parse. N.B url doesn't validate URLs, so be sure it includes a protocol or you'll get deeply weird results.

const serviceUrl = getenv.url('SERVICE_URL');

serviceUrl.port; // parsed port number

env.disableFallbacks()

Disallows fallbacks in environments where you don't want to rely on brittle development defaults (e.g production, integration testing). For example, to disable fallbacks if we indicate production via NODE_ENV:

if (process.env.NODE_ENV === 'production') {
  getenv.disableFallbacks();
}

env.disableErrors()

getenv won't throw any error. If a fallback value is provided, that will be returned, else undefined is returned.

getenv.disableErrors();
console.log(getenv('RANDOM'));
// undefined

env.enableErrors()

Revert the effect of disableErrors().

getenv.disableErrors();
console.log(getenv('RANDOM'));
// undefined

getenv.enableErrors();
console.log(getenv('RANDOM'));
// Error: GetEnv.Nonexistent: RANDOM does not exist and no fallback value provided.

Changelog

v1.0.0

  • Drop support for Node.js older than 6.
  • Modernize code.
  • Add MIT License in package.json and LICENSE.md.

v0.7.0

  • Add env.disableErrors() / getenv.enableErrors() support.

v0.6.0

  • Added getenv.boolish() support.

v0.5.0

  • Add getenv.url() support.

v0.4.0

  • Add getenv.disableFallbacks() support.

v0.3.0

  • Add getenv.multi() support.

v0.2.0

  • Rename git repository

v0.1.0

  • Initial release

Authors

License

This module is licensed under the MIT license.