Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using multiple .files() ? #110

Closed
carpii opened this issue Mar 2, 2014 · 4 comments · Fixed by #117
Closed

using multiple .files() ? #110

carpii opened this issue Mar 2, 2014 · 4 comments · Fixed by #117

Comments

@carpii
Copy link

carpii commented Mar 2, 2014

I am trying to use two calls to file(), in the hope any elements present in one file will override those in the other

nconf.argv().env().file({file: 'nconf.json'}).file({file: 'host.json'});

If I util.inspect(nconf) it seems like the stores object is only able to hold one file store at a time, so nconf.json is being replaced entirely by host.json.

Is this a known issue, or have I misunderstood something?

@deckchairhq
Copy link

+1 - I've just noticed this has been replacing parts of my config files.

My config files are:

default.json: Bare bones config, things that would otherwise need to be duplicated between all 'environment specific configs'

(production | stage | development).json: Environment specific configs, DB address etc

overrides.json: This is a special config file which can be updated, it is not included in the code repository like the other files.

I specify nconf.use('memory'); at the top of my code as I believed this would ensure no writing to any files...

How would you advise I go about this?

@deckchairhq
Copy link

Temporary workaround:


    var localConfigPath = Path.resolve( '..my_path...', 'config'),

        defaultConfigPath = Path.resolve(localConfigPath, 'default.json'),
        defaultConfig = require( defaultConfigPath ),

        environmentConfigPath = Path.resolve(localConfigPath, nconf.get('environment') + '.json'),
        environmentConfig = require( environmentConfigPath ),

        remoteConfigPath = Path.resolve( '..my_path...', 'RemoteConfig.json'),
        remoteConfig = require( remoteConfigPath );


    nconf
        .add( 'remote', {
            type:'literal',
            store: remoteConfig
        })
        .add( nconf.get('environment'), {
            type:'literal',
            store: environmentConfig
        })
        .defaults({
            store: defaultConfig
        });

@vegar
Copy link

vegar commented Mar 13, 2014

I think this is a duplicate of issue #97.

Shorter answer is that

var nconf = require('nconf');
nconf.file('overrides', 'overrides.json')
         .file('environment', development.json')
         .file('defaults', 'default.json');

should give you the effect you ask for.

Why is explained in my answer for #97.

MitchellMcKenna added a commit to MitchellMcKenna/nconf that referenced this issue Jun 18, 2014
- Update the Readme with notes that when using multiple config files,
  the user must use a custom key as the first parameter in additional
  nconf.file() calls in order for config heirarchy/inheritance to work
  properly.

Fixes indexzero#97, Fixes indexzero#109, Fixes indexzero#110
@marcellodesales
Copy link

I ran into a situation that the name of the store being "overrides" and "defaults" completely wipes out the stores for the call nconf.defaults() and nconf.overrides().

@vegar is this correct? I did the following to prevent that:

/**
 * Loads a given file based on the name.
 *
 * @param {string} appConfigDir the path to the file.
 * @param {string} fileName the name of the file.
 * @param {string} format is the format to be loaded.
 */
function _loadFile(appConfigDir, fileName, format) {
  var filePath = path.resolve(appConfigDir, fileName + "." + format);
  log.debug("Attempting to load the " + fileName + " file at " + filePath);

  if (format === "json") {
    nconf.file(fileName + "-store", filePath);

  } else {
    // load a yaml file using a custom formatter
    // https://gist.github.com/clarkdave/f31d92ca88d11ef5340c
    nconf.file({
      file: filePath + "-store",
      format: {
        parse: yaml.safeLoad,
        stringify: yaml.safeDump,
      }
    });
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants