npm
Source
Description
Functional utilities for reading and parsing environmental variables, based on Wolfstar’s internal tools.Usage
Setup
To setup@wolfstar/env-utilities, you use the setup function exported by the package:
string or if you want to define other options, you may use EnvSetupOptions. Optionally, you may configure dotenv via environment variables:
DOTENV_DEBUG: configuresEnvSetupOptions.debug. If enabled, the library will log to help debug why certain keys or values are not being set as expected.DOTENV_ENCODING: configuresEnvSetupOptions.encoding. If set, it will specify the encoding of the files containing the environment variablesDOTENV_ENV: configuresEnvSetupOptions.env. If set, it will specify a custom environment ifNODE_ENVis not sufficient.DOTENV_PATH: configuresEnvSetupOptions.path. If set, it will specify a custom path to the file containing environment variables, useful for when they are located elsewhere.DOTENV_PREFIX: configuresEnvSetupOptions.prefix. If set, it will specify a required prefix for dotenv variables (e.g.APP_).
What .env files can be used?
Every file below is searched first under src/, then at the project root. An explicit path or DOTENV_PATH
disables this discovery and uses that base path only.
.env: Default..env.local: Local overrides. This file is loaded for all environments except test..env.development,.env.test,.env.production: Environment-specific settings..env.development.local,.env.test.local,.env.production.local: Local overrides of environment-specific settings.
npm start:.env.development.local,.env.local,.env.development,.envnpm test:.env.test.local,.env.test,.env(note.env.localis missing)
Typing Environment Variables
To add new entries, you augmentEnv from @wolfstar/env-utilities/dist/lib/types using any of the following types:
BooleanString: can be parsed withenvParseBoolean.IntegerString: can be parsed withenvParseInteger.NumberString: can be parsed withenvParseNumber.string: can be parsed withenvParseStringandenvParseArray.
ReferenceError instance if a key is missing (unless a default is passed in the second parameter) as well as a TypeError instance if a key could not be parsed. The default value is returned as-is and is not validated.
An example of adding more keys is as it follows: