How to use the Provider function from nconf

Find comprehensive JavaScript nconf.Provider code examples handpicked from public code repositorys.

nconf.Provider is a class in the nconf library that provides a configurable hierarchical store for application settings and configuration data.

14
15
16
17
18
19
20
21
22
23
debug('config start');
options = options || {};

const baseConfigPath = options.baseConfigPath || __dirname;
const customConfigPath = options.customConfigPath || process.cwd();
const nconf = new Nconf.Provider();

// ## Load Config

// no channel can override the overrides
fork icon0
star icon1
watch icon0

+ 4 other calls in file

222
223
224
225
226
227
228
229
230
231
/**
 * Constructor
 */
constructor() {
    // create own instance in case other dependencies also use nconf
    this._config = new nconf.Provider();

    ///////////////////////////////////////////////////////////////////////////////
    // the priority is the following:                                            //
    // memory > commandline args > environment variables > project config file > //
fork icon0
star icon0
watch icon1

+ 10 other calls in file

How does nconf.Provider work?

nconf.Provider works by creating a hierarchical store for application settings and configuration data, with the ability to load and save data from various sources.

When instantiated, the nconf.Provider object provides a set of methods for reading and writing configuration data, such as set(), get(), and load().

The nconf.Provider object also supports a series of "stores" that allow the loading and saving of configuration data from various sources, such as environment variables, command-line arguments, JSON files, and more.

The hierarchical structure of the store allows for cascading configuration values, where the values in a higher-priority store override those in lower-priority stores.

For example, if a configuration value is set in both a JSON file and an environment variable, the value in the environment variable will override the value in the JSON file.

By using nconf.Provider, developers can easily manage and organize application settings and configuration data, and provide a flexible and configurable way to load and save configuration data from various sources.

84
85
86
87
88
89
90
91
92
this.appName = options.appName;
this.learnDirectoryAndFilename = getLearnFilename(options.appName, options.learnDirectory);
this.baseFilesDir = options.baseFilesDir || process.cwd();

const logger = this.logger = logging.getLogger('config');
const store = this.store = new nconf.Provider();

const baseConfigDir = this.baseConfigDir = options.baseConfigDir || process.cwd();
logger.debug('Init with baseConfigDir: ' + baseConfigDir);
fork icon0
star icon0
watch icon0

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const nconf = require("nconf");

// Create an nconf.Provider object
const provider = new nconf.Provider();

// Set up the configuration stores
provider.argv().env().file({ file: "./config.json" });

// Set a configuration value
provider.set("database:host", "localhost");

// Get a configuration value
const host = provider.get("database:host");
console.log(`Database host: ${host}`);

// Load the configuration data from all stores
provider.load((err, data) => {
  if (err) {
    console.error(err);
  } else {
    console.log("Configuration data loaded successfully");
  }
});

In this example, we use nconf.Provider to manage and organize application settings and configuration data. We first create an nconf.Provider object and then use the argv(), env(), and file() methods to set up the configuration stores from which data can be loaded. We then set a configuration value using the set() method, and retrieve it using the get() method. Finally, we load the configuration data from all stores using the load() method, which takes a callback function that is called when the loading is complete. By using nconf.Provider in this way, we can easily manage and configure application settings and configuration data, and provide a flexible and configurable way to load and save configuration data from various sources.