Can someone please explain my why this happens and how to make it work correctly:
Imagine we have a module published on NPM called test-module with the following content:
function Class() {
}
module.exports = Class;
Then let's imagine our project folder looks like this:
[root]
-- child
and let's assume both the root and child folders have their own npm packages installed including our test-module module (same version of the module used in both folders).
Then we have the following files in the project:
// [root]/index.js
var Test = require("test-module");
var childTest = require("./child");
var one = new Test();
childTest(one);
and
// [root]/child/index.js
var Test = require("test-module");
var two = new Test();
module.exports = function(test) {
console.log("#1:", test instanceof Test);
console.log("#2:", two instanceof Test);
}
If we were to run node index.js on the root folder the result in the console is this:
#1: false
#2: true
Question(s)
Shouldn't both #1 and #2 statements equal to true? Why the statement marked as #1 in the console log equals to false even though test argument (one) is actually the instance of our test-module module, it just isn't installed in the same folder? Is there a way to make it work like this without some hackish solutions that came up to my mind?