Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
getenv
Advanced tools
Readme
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.
npm install getenv
TypeScript types are available from the @types/getenv
module.
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);
});
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.
Alias for env.string(name, [fallback])
.
Return as string.
Return as integer number.
Return as float number.
Return as boolean. Only allows true/false as valid values.
Return as boolean. Allows true/false/1/0 as valid values.
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
.
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
});
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
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();
}
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
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.
getenv.multi()
support.disableFallbacks()
, url()
disableErrors()
, enableErrors()
This module is licensed under the MIT license.
FAQs
Get and typecast environment variables.
The npm package getenv receives a total of 710,678 weekly downloads. As such, getenv popularity was classified as popular.
We found that getenv demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
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.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.