lighter-config module loads configuration files and allows JSON to be
decorated with environment variables for
"production" environments, according to the
ENV environment variables.
From your project directory, install and save as a dependency:
npm install --save lighter-config
Create the following files in your project directory, with some JSON content:
Then in your project, from a file such as
/index.js, require the
lighter-config module, and use the result as your configuration.
var config =// Example usage of config:if configisDevelopmentconsole// And if base.json or development.json has a "host" and "port" property:console
The result of requiring
lighter-config is an object with the following
base properties, as well as any properties found in configuration files:
env is found from an environment variable, as described below, or from
env property of the options argument to the
get method as described
below. If omitted, it defaults to "staging".
environment is based on the
env, and is coerced to be either
"development", "staging" or "production".
isDebug property is a special property which is true if the
something like "debug" or "dbg".
isProduction properties are based on the
environment. Exactly one of them is
lighter-config library returns an object which can be used to load other
configurations using the
get method. The options object can have the
config.envvalue (with the default being "staging").
dirdetermines which directory JSON files will be loaded from (with the default being "config"). The value is used to prefix calls to
fs.readFileSync, so it can be an absolute path.
basedetermines the name of the base configuration, such as
base.json, which is loaded prior to the environment-specific configuration, such as
staging.json. It defaults to "base", making the base configuration file
var config =var prodConfig = config
load method can load a named configuration from a directory, and use it
to override a configuration object's properties.
If a configuration file contains a key called
subEnvironments with an array
of sub-environment names, then those environment names are whitelisted as
"key": "p""subEnvironments": "pre-production"
NODE_ENV is set to "pre-production", then the "pre-production.json"
file will be loaded and used to override the "production.json" configuration.
You can affect the outcome of
lighter-config by running your application with
specific environment variable values, or modifying
process.env prior to
lighter-config will look for configuration JSON files.
Defaults to "config" if not found.
Determines the name of the base configuration. Defaults to "base".
Determines the value of
config.env directly, and
indirectly. Defaults to "staging" if not found.
Configuration files can include replacement variables, for which substitutions
will be made. For example, if you want to expect a host and port to be in the
PORT environment variables, you can provide the following JSON:
You can also include default values: