# Configuration

Nodewood stores its common configuration values files in @app/config and @wood/config. Defaults are stored in the #wood version of the file, while application-specific overrides can be placed in the #app versions.

WARNING

These configuration files, like much of the rest of Nodewood, can be loaded by either the back-end API or the front-end UI. This means that any secrets you place in them can be potentially read by curious users who view the source of your application!

To keep critical back-end secrets secret, don't store them in these files. Instead, store them in #config-api/, and use getConfigApi() from #lib/Config instead of getConfig(). This function will load files from the correct folder, and contains protections to prevent it from being called successfully in webpack. This will prevent values from these files from showing up in your UI front-end package.

# Loading configuration values

To load a configuration value, require the getConfig() function from #lib/Config, then use as follows:

  const appName = getConfig('app', 'name', 'A default app name');

This will load the name value from the #config/app file. If no name value is found, A default app name is returned instead.

The second parameter can also be used to specify a deeper path than just top-level:

  const appFeatures = getConfig('app', 'features.wood');

This will load the wood value of the features object in the #config/app file. Internally, this uses Lodash's get function (opens new window), so all of its path semantics will work here as well.

# Overriding configuration values

When testing, you will occasionally want to override configuration values to make testing simpler. To do this, require the overrideConfig() function from #lib/Config, and use it as you would getConfig():

  overrideConfig('app', 'name', 'The replacement name');

From here, when you use getConfig() to get the configuration value, it will return the overridden value until you call clearConfigOverrides() (generally added to your test's afterEach() method).

If you wish to override an entire file (for example, if you wish to use simplified Stripe config files while testing), you can do this with overrideConfigFile():

  const fixtureStripeCoupons = require('@wood/config/stripe/__fixtures__/coupons.json.fixture.js');
  overrideConfigFile('stripe/coupons', fixtureStripeCoupons);