A new instance of third-party module should be required in one of the modules.
// a
...
exports.thirdParty = require('third-party');
// b
...
exports.thirdParty = require('third-party');
// first-party
...
exports.thirdParty = requireFreshInstance('third-party');
// app.js
var assert = require('assert');
var a = require('a');
var firstParty = require('first-party');
var b = require('b');
assert(a.thirdParty === b.thirdParty);
assert(firstParty.thirdParty !== a.thirdParty);
assert(firstParty.thirdParty !== b.thirdParty);
All of the listed modules have similar package requirements,
dependencies: {
"third-party": "^1"
}
And the requirement is that it should stay intact, no tricks like "third-party": "git://..."
are allowed.
Let's say the user controls only first-party
module and can't modify third-party
module to have a new factory
method that would return a new instance.
I'm aware of the fact that third-party
is cached once if the version is the same in all cases (technically it is full path to third-party
that matters), most likely .thirdParty
properties in all objects are equal.
How can this problem be solved in Node.js programmatically (not with package.json)?