Strict Mode

Turn on strict mode to treat whitespace as missing, throw on unresolved templates, and reject empty array elements.

Strict mode is a global switch that tightens how envapt treats whitespace, unresolved templates, and empty array elements. It's off by default.

. = true;

Setting it refreshes the cache, so values already read are re-evaluated under the new rule.

What strict mode changes

Three behaviors tighten when strict mode is on:

  1. Whitespace counts as missing. Normally only an unset variable or an empty string is missing. Under strict mode a value that is only whitespace is missing too, so it falls back the same way an unset variable would.
  2. Unresolved templates throw. A ${VAR} reference that can't be resolved is left as literal text by default. Under strict mode it throws EnvaptError with code MissingEnvValue. The same applies to a missing key in the resolve tagged template.
  3. Empty array elements throw. Converters.array drops empty elements by default. Under strict mode an empty element (a trailing comma, say) throws EnvaptError with code EmptyArrayElement.
NOTE

Strict mode is global and retroactive: the setter clears the cache, so the next read of any value re-runs under the new rule. Set it once, early, alongside your other envapt configuration.

Strict mode does not make every read throw

A plain reader still returns the fallback (or undefined) for a missing value, even under strict mode. Strict mode changes how whitespace, templates, and array elements are treated; it does not turn every missing variable into a throw.

To fail fast on a specific value, use require or the { required: true } form. These throw on a missing value regardless of strict mode.

// throws if unset, independent of Envapter.strict
const dbUrl = .('DATABASE_URL', { : ., : true });
const dbUrl: URL
.('API_KEY', 'SENTRY_DSN');
TIP

Use required / require when one specific value must be present. Turn on strict mode when you want stricter whitespace, template, and array-element rules everywhere.

See Fail-fast on missing values for the reader API and Errors for the codes.

On this page