Reputation: 51
I have those files
/Controller/sub_module/conversation.js
var test = [];
function push(){
test.push(test.length);
}
module.exports = {
push,
getTest(){
return test;
}
}
/index.js (at root)
require('../chatserver/Controller/sub_module/conversation').push();//push new item to test variable
and then in /Controller/index.js
const test1 = require('../Controller/sub_module/conversation').getTest();//return [0]
const test2 = require('./sub_module/conversation').getTest(); //return []
test1 and test2 not point to same reference, why ? Node 8.11.3 IDE Webstorm Debug image
Upvotes: 0
Views: 89
Reputation: 51
Thanks to @T.J.Crowder for this : https://nodejs.org/api/modules.html#modules_module_caching_caveats.
I think all relative paths will be converted to absolute paths for name-caching, and in this conversion, the unspecified part of the relative path will be auto-converted to lowercase.
Because that Node will consider
require('./sub_module/conversation') //the unspecified part is 'Controller' will be convert to 'controller'
same with
require('../controller/sub_module/conversation')
but different with
require('../Controller/sub_module/conversation')
Upvotes: 2
Reputation: 1101
require
d modules shouldn't be used as singleton storages. In node, require()
returns the same reference to the same module ONLY if the files you're requiring the module from reside in the same directory. Otherwise, even NPM modules are different as detailed in This post.
Upvotes: -1