Node.js caching of required scripts

2019-04-25T01:37:00.000-07:00

var test = require('./modules/test/test2.js');
console.log('expect world', test.hello);
test.hello = "dirt";
require('./modules/testbed.js');
console.log('expect universe', test.hello);
module.exports = {
    hello: 'world'
}
var test2 = require('./test/test2.js');
console.log('expect dirt', test.hello);
test2.hello = "universe";
index.js:2   expect world world
testbed.js:2 expect dirt dirt
index.js:5   expect universe universe

Giving me confidence that it doesn't matter from which file or directory you require the module, as long as the filename is the same.

The only caveat is that if you use a case insensitive file system, things will break down if you're not consequent in your filename strings.

For example: if you change testbed.js to contain this:

var test2 = require('./test/Test2.js');// Note the uppercase T
console.log('expect dirt', test2.hello);
test2.hello = "universe";
index.js:2   expect world world
testbed.js:2 expect dirt world
index.js:5   expect universe dirt