
Security News
OWASP 2025 Top 10 Adds Software Supply Chain Failures, Ranked Top Community Concern
OWASP’s 2025 Top 10 introduces Software Supply Chain Failures as a new category, reflecting rising concern over dependency and build system risks.
@bolajiolajide/env-utils
Advanced tools
Easily Get Environment Variables
yarn add @bolajiolajide/env-utils
Gets an environment variable. This returns an error if the environment variable isn't detected.
import getEnvVar from 'env-utils';
getEnvVar(envVarName, options);
options.isBoolean - Forces the value to be a boolean// returns true if the variable is 'true' else it returns false
const shouldAcceptCoins = getEnvVar('SHOULD_ACCEPT_COINS', { boolean: true });
options.isArray - If your env variable is a comma separated string you can get back an array instead.// process.env.PORTS = '8080,9000,3000'
const PORTS = getEnvVar('PORTS', { isArray: true }); // returns ['8080', '9000', '3000'];
In the event that the variable is separated by something other than a comma, you can define the separator using options.separator.
// process.env.PORTS = '8080&9000&3000'
const PORTS = getEnvVar('PORTS', { isArray: true, separator: '&' }); // returns ['8080', '9000', '3000'];
options.devDefault - used to specify a development-environment-only fallback for the variable. If the variable is undefined, the devDefault is returned in it's stead.
This only applies when process.env.NODE_ENV === 'development'. Any other value of NODE_ENV will not regard this optionconst PORT = getEnvVar('PORT', { devDefault: '1234'});
// if process.env.PORT is not set, the value of PORT will be `1234`
options.optional - Used to identify variables that are optional. This is specifically useful when you don't want an error thrown for undefined variables.const SOMETHING = getEnvVar('SOMETHING', { optional: true });
// if process.env.SOMETHING is undefined, the value of SOMETHING will be undefined.
// else it'll be whatever the value of process.env.SOMETHING is
options.isNumber - used to convert numeric-like variables into number. Note: This will throw an error if it's true and the variable isn't numeric-like.// if process.env.PORT = '8080'
const PORT = getEnvVar('PORT', { isNumber: true }); // returns 8080
// if process.env.PORT = 'smash' - not a valid number
const PORT = getEnvVar('PORT', { isNumber: true }); // this will result in an error being thrown
You can lazy fetch an environment variable using the function below, it's a result of composing the getEnvVar method:
const lazyGetEnvVar = (...args) => () => getEnvVar(...args);
env-utils exports some utility functions that return variables in a certain type. This is especially useful for typescript projects, so you don't have to deal with type casting with the as keyword.
Below is an example:
import getEnvVar, { getStringEnv, getNumericEnv } from '@bolajiolajide/env-utils';
getEnvVar('SENTRY_DSN') // returns a value of type EnvValue | EnvValue[] | undefined
// you'd need to manually type cast to a string `getEnvVar('SENTRY_DSN') as string` to avoid ts errror
/*
* You can make use of the utility functions and they return the appropriate types
*/
getStringEnv('SENTRY_DSN') // will always return a string | undefined
N.B All utility functions have exactly the same signature as the getEnvVar function, along with some overrides that ensures that there are no implicit type conversion/casting.
getStringEnv returns an environment variable as a stringgetArrayEnv<T> returns an environment variable as an array, can be passed a generic to identify the type of the array's contentgetBoolEnv returns an environment variable as a booleangetNumericEnv returns an environment variable as a numberThis works great with the lazy-config module. Hello World
FAQs
Smart Environment Utilities for faster operations
We found that @bolajiolajide/env-utils 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
OWASP’s 2025 Top 10 introduces Software Supply Chain Failures as a new category, reflecting rising concern over dependency and build system risks.

Research
/Security News
Socket researchers discovered nine malicious NuGet packages that use time-delayed payloads to crash applications and corrupt industrial control systems.

Security News
Socket CTO Ahmad Nassri discusses why supply chain attacks now target developer machines and what AI means for the future of enterprise security.