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

Is it possible to force a reload of the config? #34

Closed
nickminutello opened this issue Oct 1, 2012 · 5 comments
Closed

Is it possible to force a reload of the config? #34

nickminutello opened this issue Oct 1, 2012 · 5 comments

Comments

@nickminutello
Copy link

I am writing a test for our various different configurations - to ensure that after all the different host/environment/etc configs are loaded and merged, we end up the final config we expect.

e.g.
_.each(configs, function(envConfig){
var actualConfig = require(envConfig);
var expectedConfig = readExpectedConfig(envConfig);
test.deepEqual(actualConfig, expectedConfig);
test.done();
})

(I'm manipulating environment variables to

However, I cant seem to load more than 1 config because of the singleton nature of it.
Is there a way to do it already?
If not, should we add one?

@lorenwest
Copy link
Collaborator

It is designed to have one config per application instance, with the singleton pattern used so different sub-modules can share a common configuration.

In your case, you may be able to reset configs by setting environment variables, then calling global.NODE_CONFIG = new Config(); in your loop (assuming you've set Config = require('config') at the top of your test).

It's not designed for that, but that might get you what you need. I'm hesitant to build-in support for multiple configs because it seems like a test-only situation.

Let me know if that works for you.

@nickminutello
Copy link
Author

Config = require('config')
global.NODE_CONFIG = new Config();

No, that doesnt work.
TypeError: object is not a function.
module.exports is assigned the config object - not a function.

It is designed to have one config per application instance, with the singleton
pattern used so different sub-modules can share a common configuration.
I'm hesitant to build-in support for multiple configs because it seems like a test-only situation.

I understand the intent of the singleton - and the simplicity of use across modules.
But being able to test is also important for us.
We have 6 different environments (2 prod/uat & 3 regions) - and are using all levels of config available (default + env + host).
Without tests I'm finding it hard to re-factor or reorganise our config without introducing inadvertent changes...

@lorenwest
Copy link
Collaborator

Here's another approach - The config module is cached by Node in the require module. Theoretically you should be able to remove all traces and reload by doing this within your loop:

...reset your environment variables...
global.NODE_CONFIG = null;
delete require.cache.config;
var config = require('config');
...test the new configs...

Testing is very important - let me know how this works for you.

@nickminutello
Copy link
Author

That works with one small change. Instead of delete.require.cache.config, we need:

    delete require.cache[require.resolve('config')];

Thanks!

@lorenwest
Copy link
Collaborator

Glad we came up with a solution - thanks for your help as well!

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

2 participants