1

in my app I share config variables this way:

const config = require('./config')

and config.js is a separate file:

module.exports = {
  option1: value1,
  option2: value2
};

If I change attributes in config object dynamically, does it affect other modules as well?

for example:

config.js

module.exports = { foo: 'bar' }

app1.js

var config = require('./config');
module.exports.sayFoo = function () {
    console.log(config.foo);
}

app2.js

var config = require('./config');
var app1 = require('./app1');
config.foo = 'baz';

app1.sayFoo();


# node app2.js =====> ??
1
  • 5
    Since you've got all the code in front of you, why not run it and see? Surely faster than asking a question? Commented May 11, 2016 at 10:14

2 Answers 2

1

require in Node caches the response so if you call the same thing twice, it will use the cached version.

However, thkang is right. Just run it and you will see it for yourself.

Sign up to request clarification or add additional context in comments.

Comments

1

Write. I'll see it myself.

I've tested two cases:

1) can you dynamically change a required module's attribute?

2) can you dynamically change what modules exports?

config.js

module.exports = { foo: 'bar' }

app1.js

var config = require('./config');
var app2 = require('./app2');
config.foo = 'baz'; // change required module attribute

app2.sayFoo();

console.log(app2.haveFoo);

app2.js

var config = require('./config');
module.exports.sayFoo = function () {
    console.log(config.foo);
    module.exports.haveFoo = true; //dynamic export
}

results

# node app1.js
baz
true

both cases work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.