Skip to main content

EnvironmentSource

The EnvironmentSource loads configuration assignments from environment variables. By default, the environment variable name is formed by combining the appName value (passed in the context) with the dotted field path (from the schema), and converting the whole thing to CONSTANT_CASE.

In the case that a child name at the start of the path has the same name as appName (it is often useful to encapsulate the app's configurables into a child schema!), the name is truncated to avoid redundancy.

For example, given the app called basics defined in the Getting Started section, here are some environment variables that would be loaded:

BASICS_DEBUG=true
BASICS_VERBOSE=true
BASICS_CODES=5xx,z10,123
BASICS_SERVER_HOST=localhost
BASICS_SERVER_PORT=8081
BASICS_SERVER_PROTOCOL=https

(If we didn't truncate the redundant name segment, we'd need to use BASICS_BASICS_VERBOSE=true!)

If you don't provide an appName in the context, the environment variables will not have a prefix at all, which may lead to surprising results if you inadvertently define a field with the same name as a common shell variable.

By default, the EnvironmentSource will load data from process.env. However, it is possible to pass in an object containing values via a property in the context object passed to Configurator.configure(), by default context.env. (This is used in the examples directory to ensure a consistent environment for demo purposes.)

For example,

  const config = await new Configurator({schema}).configure({
appName: 'basics',
env: {'BASICS_SERVER_HOST': '127.0.0.1'} // normally omit
});