Skip to content

Load configuration from environmental variables and files.

License

Notifications You must be signed in to change notification settings

tomtum1987/env-and-files

 
 

Repository files navigation

env-and-files

Load configuration from environmental variables and files.

According to The Twelve-Factor App, configuration should come from environmental variables. But since environmental variables can leak easily, some people use secrets for sensitive information. This module is made to support either with minimal set-up.

Install

$ yarn add env-and-files

Usage

const {loadConfig} = require('env-and-files');

loadConfig({
  // A conceptual grouping of configuration properties.  In this case, configuration for the logger.
  logger: {
    // The "logger.level" property will be equal to the "LOG_LEVEL" environmental variable, or null if it is not present.
    level: 'LOG_LEVEL',
  },
  server: {
    port: {
      // Specify that this property is required.  If "PORT" is not found, an error will be given.
      required: true,
      variableName: 'PORT',
    },
  },
  sql: {
    password: {
      // The "sql.password" property will be equal to the contents of "/path/to/secret", or null if it could not be read.
      filePath: '/path/to/secret',
      required: true,
    },
  },
})
  .then(config => {
    // "config" will be an object map of configuration groups.  In this case, the shape would be:
    // { logger: { level: ?string }, server: { port: ?string }, sql: { password: ?string } }
    console.log(config);
  })
  .catch(error => {
    // If any of the required properties cannot be loaded, the Promise will reject.
    console.log(error);
  });

API

loadConfig(configMap)

Load configuration. Returns a Promise that will resolve to the loaded configuration, or reject if the configuration was invalid.

configMap

Type: Object

An object map of conceptual groupings of necessary configuration and where to find it. By default, all configuration properties are optional, but if one is marked required and is not found, an error will be given. See usage for examples of config maps.

loadConfigSync(configMap)

Load configuration, synchronously. Returns the loaded configuration, or throws if the configuration was invalid.

configMap

Type: Object

Same as the asynchronous version.

License

MIT © Matthew Fernando Garcia

About

Load configuration from environmental variables and files.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%