shared.js exports an object, which will be required and mutated.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// first import of shared | |
var shared = require('./shared'); | |
// mutate the required module | |
shared.mutated = 'mutated'; | |
module.exports = null; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// load module that loads shared.js | |
var mutateRequired = require('./mutate-require'); | |
// load shared, which will hit cache | |
var cached = require('./shared'); | |
console.log(cached); | |
/* | |
$ node -v | |
v5.2.0 | |
$ node index.js | |
{ one: 'one', mutated: 'mutated' } | |
*/ |
Is this the behavior you'd expect? I was surprised, when I saw this. I could imagine a more sane default being: having `require` copy the exported value to the cache and returning the clean copy on each require. But then what about modules that export objects that mutate themselves?? Thoughts?