Socket
Socket
Sign inDemoInstall

getenv

Package Overview
Dependencies
0
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    getenv

Get and typecast environment variables.


Version published
Maintainers
1
Install size
20.7 kB
Created

Readme

Source

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

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:

var getenv = require('getenv');

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

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

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

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

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

var 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.

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:

var 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, "integer"] // parse & throw
});

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();
}

Changelog

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.

Keywords

FAQs

Last updated on 24 Nov 2014

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc