I'm developing a node module my-module
which in turn depends on another module other-module
. other-module
is thus a dependency explicitly listed in my module's package.json.
As my module modifies the behaviour of other-module
just by being require
d, it is important that other-module
is loaded only once and that this, one-and-only 'instance' is the one referenced throughout any application that requires both my
and other
.
I expected this to hold true according to node's Module Caching Policy but what I've come across while writing a simple test app is this:
If my-module
is npm install
ed before other-module
then the latter is brought in as a dependency of the former. npm install
ing other-module
afterwards brings it into the node_modules hierarchy a second time. Then, when my module requires other-module
, node loads my module's 'local' copy and when the app require
s it a second time node loads it again, (this time the version that was installed due to the second npm install
). This obviously is not the intended result.
If my-module
is npm installe
d after other-module
then I end up with only one copy of other-module
in node_modules and my test app works as expected.
This behaviour got me looking through node's relevant policies again and sure enough I came across the 'Module Caching Caveats':
Modules are cached based on their resolved filename. Since modules may resolve to a different filename based on the location of the calling module (loading from node_modules folders), it is not a guarantee that require('foo') will always return the exact same object, if it would resolve to different files.
At this point it looks like that my module may or may not behave as expected depending on the order of npm install
s.
Are there any best practices I'm missing? Is there any way to avoid this mess without changing the way my module works?
The short answer: You can not.
As you pointed out node will load the required module from the most local place. This is as far as I know unique to a package manager and it enables you to don't care about the exact dependency tree of your modules. Node and npm will figure that out for you. In my opinion this is something really good.
Dependency hell is simply avoided by giving your modules the opportunity to require an exact version of what they need.
I think what you are trying to do, unless I do not understand your question fully, is not good node practice. Modules are loaded and assigned to a local variable. Global state should be avoided, as this can lead to rather awkward and untestable code. Additionally if you would succeed to inject your modified module into other's people code, there could be no guarantee that their code would still work. This would be as in the old Prototype.js_ days when it was ok to hack around with JavaScript's built-in globals like String or Array, which led to some disastrous code.
However keep in mind that this writing is just the opinion of one person. If you don't find more answers here, post your question otherwhere like the node's IRC channel.