What is getenv?
The 'getenv' npm package is a utility for safely and easily accessing environment variables in Node.js applications. It provides a straightforward API to retrieve environment variables with type safety and default values.
What are getenv's main functionalities?
Get a string environment variable
This feature allows you to retrieve a string environment variable. If the variable is not set, it will throw an error.
const getenv = require('getenv');
const dbHost = getenv('DB_HOST');
Get a string environment variable with a default value
This feature allows you to retrieve a string environment variable and provide a default value in case the variable is not set.
const getenv = require('getenv');
const dbHost = getenv('DB_HOST', 'localhost');
Get an integer environment variable
This feature allows you to retrieve an integer environment variable. If the variable is not set or cannot be converted to an integer, it will throw an error.
const getenv = require('getenv');
const port = getenv.int('PORT');
Get an integer environment variable with a default value
This feature allows you to retrieve an integer environment variable and provide a default value in case the variable is not set or cannot be converted to an integer.
const getenv = require('getenv');
const port = getenv.int('PORT', 3000);
Get a boolean environment variable
This feature allows you to retrieve a boolean environment variable. If the variable is not set or cannot be converted to a boolean, it will throw an error.
const getenv = require('getenv');
const isProduction = getenv.bool('IS_PRODUCTION');
Get a boolean environment variable with a default value
This feature allows you to retrieve a boolean environment variable and provide a default value in case the variable is not set or cannot be converted to a boolean.
const getenv = require('getenv');
const isProduction = getenv.bool('IS_PRODUCTION', false);
Other packages similar to getenv
dotenv
The 'dotenv' package loads environment variables from a .env file into process.env. It is widely used for managing environment variables in Node.js applications. Unlike 'getenv', 'dotenv' does not provide type safety or default values.
env-var
The 'env-var' package provides a similar functionality to 'getenv' with additional features like type validation, default values, and custom transformations. It offers a more comprehensive API for handling environment variables.
config
The 'config' package allows you to manage configuration files for your Node.js application. It supports environment-specific configurations and provides a more structured approach to managing environment variables compared to 'getenv'.
getenv
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');
const port = getenv.int('HTTP_PORT');
const start = getenv.bool('HTTP_START');
if (start === true) {
}
const abTestRatio = getenv.float('AB_TEST_RATIO');
if (Math.random() < abTestRatio) {
} else {
}
const keywords = getenv.array('KEYWORDS');
keywords.forEach(function(keyword) {
});
const primes = getenv.array('PRIMES', 'int');
primes.forEach(function(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',
bar: ['BAR', 'defaultval'],
baz: ['BAZ', 'defaultval', 'string'],
quux: ['QUUX', undefined, 'int'],
});
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;
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'));
env.enableErrors()
Revert the effect of disableErrors()
.
getenv.disableErrors();
console.log(getenv('RANDOM'));
getenv.enableErrors();
console.log(getenv('RANDOM'));
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
v0.1.0
Authors
License
This module is licensed under the MIT license.