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

Calling config.util.setModuleDefaults(namespace, localOptions) a second time crashes #546

Open
bodaro opened this issue Mar 31, 2019 · 5 comments

Comments

@bodaro
Copy link

bodaro commented Mar 31, 2019

I'm submitting a ...

  • [X ] bug report
  • [ ] feature request
  • [ ] support request or question => Please do not submit support request or questions here, see note at the top of this template.

What is the current behavior?

Calling config.util.setModuleDefaults(namespace, localOptions) a second time with a different value for namespace causes this error:

    TypeError: Cannot read property 'config' of undefined
at node_modules/config/lib/config.js:1374:37
          at Array.forEach (<anonymous>)
      at Config.Object.<anonymous>.util.extendDeep (node_modules/config/lib/config.js:1367:9)
      at Config.Object.<anonymous>.util.setModuleDefaults (node_modules/config/lib/config.js:289:8)
      at Object.setModuleDefaults [as config] (lib/util.js:39:15)

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem: Please see other information.

What is the expected behavior?

Calling a second time with a different value for a namespace will allow me to access the config items in that namespace by doing config.get('namespace2.XXX')

Please tell us about your environment:

  • node-config version: 3.0.1
  • node-version: v8.15.0

Other information

Here is a function I wrote that is intended to allow me to read an arbitrary config directory (say in another module or place on disk from the application's config directory) and put those config settings into a namespace inside the global config object.

const config = require('config')

// load a config directory and deep merge in other properties
// if namespace was 'dtc'db', now the application can do `config.get('dtc-db.accounts')`
function _config(dir, namespace, extendWith = {}) {
  process.env.SUPPRESS_NO_CONFIG_WARNING = 'y'
  // load configs from any directory
  const localOptions = config.util.loadFileConfigs(dir)
  // apply extendWith options (if any) passed in on top of our directory options
  config.util.extendDeep(localOptions, extendWith)
  // push those new deep merged configs into the config object
  config.util.setModuleDefaults(namespace, localOptions)
 // return the new deep merged configs
  return config.get(namespace)
}
//...
//calling the above function a second time with a different value for _namespace_ gives the error (as shown above)

   const dir = '../../../somewhere/else/config'
    util.config(dir, 'merged')
    util.config(dir, 'merged2')
@bodaro
Copy link
Author

bodaro commented Apr 6, 2019

I also noticed that calling config.util.setModuleDefaults(namespace, localOptions) a second time (with the same value for namespace) does not add the localOptions to the config object.

@Codermar
Copy link

I have the same issue. Is there any work planned around this? Any workarounds?

@lorenwest
Copy link
Collaborator

Leaving this open for someone to contribute. A failing test would be a good place to start.

The current submodule support isn't adequate for my use cases, so I'm leaving this open for someone to fix if this mechanism is working for you.

@opub
Copy link

opub commented Jun 25, 2020

I am seeing the same thing after a call like this:

config.util.setModuleDefaults("loader", { initialized: new Date() });

I get this exception:

(node:37654) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'initialized' of undefined
    at /node_modules/config/lib/config.js:1240:37
    at Array.forEach (<anonymous>)
    at Config.util.extendDeep (/node_modules/config/lib/config.js:1233:9)
    at Config.util.setModuleDefaults (/node_modules/config/lib/config.js:268:8)

Versions:

node 14.4.0
config 3.3.1

@jdmarshall
Copy link
Contributor

I was trying to create a repro case for the property masking bug a couple weeks ago, and I think I may have been running into this bug here instead.

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

No branches or pull requests

5 participants