Environment
applicationEnvironment is a singleton that filters process.env down to your app's prefix and gives typed access to it; Environment reads the current deployment stage from NODE_ENV.
In one example
import { applicationEnvironment } from '@venizia/ignis-helpers';
const jwtSecret = applicationEnvironment.get<string>('APP_ENV_JWT_SECRET');
const timeout = applicationEnvironment.get<number>('APP_ENV_TIMEOUT', { defaultValue: 5000 });The singleton is created once at module load, reading only keys that start with APP_ENV (the default prefix) from process.env. Envs is an exported alias for the same instance.
How it works
- Construction filters by prefix.
new ApplicationEnvironment({ prefix, envs })copies only the keys ofenvsthat start withprefixinto an internal map - everything else is invisible toget(). The default singleton usesprocess.env.APPLICATION_ENV_PREFIX ?? 'APP_ENV'andprocess.env. get()takes an options object, not a positional default. The signature isget<ReturnType, BeforeTransformType = unknown>(key, opts?: { defaultValue?, transform? }). Withouttransform, it returns the raw value (still astring) ordefaultValuewhen the key is missing. Withtransform, it callstransform(rawValue)and falls back todefaultValueif that returnsundefinedornull.get<T>()is a type cast withouttransform, not a runtime conversion. Everyprocess.envvalue is astring; asking forget<number>('APP_ENV_PORT')still returns a string at runtime unless you passtransform: Number.- Stage detection is separate from the singleton.
Environment.currentreadsprocess.env.NODE_ENVdirectly (falling back to'development'when unset);Environment.is({ name })compares against it.ApplicationEnvironment.isDevelopment()is narrower - it checksNODE_ENV === 'development'exactly, so the'dev'alias returnsfalsethere even though it counts as a development stage everywhere else.
Deployment stages (Environment.*)
| Constant | Value | In DEVELOPMENT_ENVS |
|---|---|---|
LOCAL | 'local' | yes |
DEBUG | 'debug' | yes |
DEVELOPMENT | 'development' | yes |
DEV | 'dev' | yes - short spelling of development |
SIT | 'sit' | yes |
UAT | 'uat' | no |
ALPHA | 'alpha' | no |
BETA | 'beta' | no |
STAGING | 'staging' | no |
PRODUCTION | 'production' | no |
All ten stages are in Environment.COMMON_ENVS, which the Logger uses to decide whether DEBUG=true is honored. The five marked above are Environment.DEVELOPMENT_ENVS - the set IGNIS's error handler consults to decide whether a response may carry a stack trace or a raw driver message. The rule is fail-closed: alpha, beta, uat, staging, a typo'd name, and an unset NODE_ENV are all sanitized as production.
Common tasks
Read a variable with a default
defaultValue goes inside the options object, not as a second positional argument.
const port = applicationEnvironment.get<string>('APP_ENV_SERVER_PORT', { defaultValue: '3000' });Convert a value while reading it
Pass transform to parse instead of casting.
const timeout = applicationEnvironment.get<number>('APP_ENV_TIMEOUT', {
transform: value => Number(value),
defaultValue: 5000,
});Set or merge variables at runtime
set() writes a single key; merge() overwrites several at once - both bypass the prefix filter (they write directly, no startsWith check).
applicationEnvironment.set('APP_ENV_FEATURE_FLAG', 'enabled');
applicationEnvironment.merge({ envs: { APP_ENV_REGION: 'ap-southeast-1' } });Branch on the deployment stage
import { Environment } from '@venizia/ignis-helpers';
if (Environment.is({ name: Environment.STAGING })) {
// Staging-only behavior
}Use a custom prefix
Set APPLICATION_ENV_PREFIX before the first import of @venizia/ignis-helpers - the singleton is constructed at module load, so a later change has no effect on it.
APPLICATION_ENV_PREFIX=MY_APP_ENV
MY_APP_ENV_SERVER_HOST=0.0.0.0List every filtered key
const allKeys = applicationEnvironment.keys();
// e.g. ['APP_ENV_SERVER_HOST', 'APP_ENV_SERVER_PORT', 'APP_ENV_JWT_SECRET']TIP
BaseApplication validates every prefixed key at startup and throws on an empty value unless ALLOW_EMPTY_ENV_VALUE is truthy - see Application.
See also
- Application - environment validation during startup
- Helpers Overview - all available helpers
- Logger - uses
Environment.COMMON_ENVSfor debug log filtering - Error - uses
Environment.DEVELOPMENT_ENVSto gate error detail
Files:
packages/helpers/src/modules/env/app-env.ts-Environment,ApplicationEnvironment, theapplicationEnvironmentsingletonpackages/helpers/src/modules/env/types.ts-IApplicationEnvironmentinterface