Skip to content

[BUG] TypeError: Cannot redefine property when sub module is using config.get() within the module #703

@hershmire

Description

@hershmire

Describe the bug

I'm trying to use node-config within my company's ecosystem (apps and libraries) but running into an issue when I start using it within one of our libraries and a consumer instantiates the library more than once. Everything works fine if the library references the config via dot notation. However, if it references values of the config via the recommended config.get('...') method I'll get the following error:

/Users/example/config-module-example/node_modules/config/lib/config.js:423
      Object.defineProperty(object, propertyName, {
             ^

TypeError: Cannot redefine property: myModule
    at Function.defineProperty (<anonymous>)
    at Config.util.makeImmutable (/Users/example/config-module-example/node_modules/config/lib/config.js:423:14)
    at Config.get (/Users/example/config-module-example/node_modules/config/lib/config.js:170:12)
    at myModule (/Users/example/config-module-example/src/my-module.js:18:24)
    at Object.<anonymous> (/Users/example/config-module-example/src/index.js:7:1)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)

Here is a repo that demonstrates the issue with a contrived example (reference the README for more details): https://github.com/hershmire/config-module-example.

Expected behavior

I would expect that I could use the get() method off the config object based on your documentation. However, this doesn't seem possible since the .get() method will make the object and its children immutable, making it problematic when the second instance is created. I cannot find any documentation walking through a recommended approach other than what is referenced here (which I seem to be following) – https://github.com/node-config/node-config/wiki/Sub-Module-Configuration.

Please tell us about your environment:

  • node-config version: 3.3.8
  • node-version: v16.16.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions