Skip to content

Configuring from an External Source

Jason Marshall edited this page Jul 15, 2025 · 7 revisions

In a nutshell: define only external source connection settings in files, using those settings to connect to the source. Once connected, read additional configurations and add them to the config object returned from require('config');.

Make sure any external overrides are done in the application bootstrap - before anyone calls the first config.get();, because the config object is made immutable as soon as any client uses the values via get().

For periodic changes such as data sourced from version control or a Raft protocol (Consul, etc), you may be better served by making your own library, that uses config as source material and then lodash.merge() on update and lodash.get for access.

  const baseData = config.toObject();
  
  let all = lodash.merge({}, baseData));

  function onUpdate(externalData) {
    // This would misbehave if values are removed from the external source, such as a rollback
    // lodash.merge(all, externalData); // No

    all = lodash.merge({}, baseData, externalData}); // Yes
  }

  ...

    get(path) {
      return lodash.get(all, path);
    }

<>

Clone this wiki locally