How to use the file function from nconf

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

19
20
21
22
23
24
25
26
27
28
const allKeys = serverConfigKeys.concat(templateConfigKeys, clientConfigKeys);
nconf.env({whitelist: allKeys, parseValues: true});

// Read config_dev.json if in development-mode. Will not overwrite configs from env.
if (process.env.NODE_ENV === 'development') {
    nconf.file({file: path.join(paths.ROOT, 'config_dev.json'), parseValues: true});
}

// Defaults, fall back to these if they are not found in any of the previous sources
nconf.defaults({
fork icon24
star icon2
watch icon21

22
23
24
25
26
27
28
29
30
31

logger.info(`Service ${nconf.get('service:name')} started with NODE_ENV=${nconf.get('NODE_ENV')}`);

// 4º
if (nconf.get('configPath')) {
  nconf.file(`${nconf.get('configPath')}`);
  logger.info(`Using config file ${nconf.get('configPath')}`);
} else {
  logger.warn('configPath isn\'t setted');
  logger.warn('To set the config file use the argument --configPath=/path/to/config.json');
fork icon0
star icon3
watch icon3

57
58
59
60
61
62
63
64
65
66
  file.write(file.configFile(), prettyConfig(loadConfig(false)));
}

cmd.handler = function(argv) {
  session.argv = argv;
  nconf.file('local', file.configFile());

  // show all
  if (argv.key.length === 0)
    return log.info(prettyConfig(loadConfig(argv.all)));
fork icon438
star icon0
watch icon64

+ 3 other calls in file

5
6
7
8
9
10
11
12
13
14
15
16
17


const activePlugins = require('./build/active_plugins.json');


let relativePath = nconf.get('relative_path');
if (relativePath === undefined) {
    nconf.file({
        file: path.resolve(__dirname, nconf.any(['config', 'CONFIG']) || 'config.json'),
    });


    const urlObject = url.parse(nconf.get('url'));
fork icon1
star icon2
watch icon0

+ 2 other calls in file

5
6
7
8
9
10
11
12
13
14
// take priority. default.json contains most of the rarely changing 
// configuration properties, stored with the branch. Additional configuration 
// files - where present - add or override parameters, such as pre-shared 
// secrets, that should not be in source control.
process.argv.slice(2).forEach(element => {
    nconf.file(element,element);
});
nconf.file('local', 'config/local.json');
nconf.file('default', 'config/default.json');

fork icon0
star icon0
watch icon1

+ 7 other calls in file

11
12
13
14
15
16
17
18
19
20
// Your instance name could override the base url again
// so that specific instances of stage use a different base URL from the default
// switch (process.env.NODE_ENV) {
    switch ('shivani') {
    case 'production':
        return nconf.file('environment', {
            file: path.join(__dirname, './production.json'),
        });
    // break;
    default:
fork icon0
star icon0
watch icon0

+ 2 other calls in file

227
228
229
230
231
232
233
234
235
236

The [**nconf** package](https://github.com/indexzero/nconf) allows you to specify configuration data in a variety of formats, such as environment variables or the command line. This sample uses **nconf** in a basic manner to read the file **azure_search_config.json** and return that file's contents as a dictionary. Using **nconf**'s `get(key)` function, you can do a quick check that the configuration information has been properly customized. Finally, the function returns the configuration:

```javascript
function getAzureConfiguration() {
    const config = nconf.file({ file: 'azure_search_config.json' });
    if (config.get('serviceName') === '[SEARCH_SERVICE_NAME' ) {
        throw new Error("You have not set the values in your azure_search_config.json file. Change them to match your search service's values.");
    }
    return config;
fork icon0
star icon0
watch icon1