Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid node require loading same module twice

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 required, 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 installed before other-module then the latter is brought in as a dependency of the former. npm installing 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 requires 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 installed 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 installs.

Are there any best practices I'm missing? Is there any way to avoid this mess without changing the way my module works?

like image 931
biril Avatar asked Dec 17 '12 00:12

biril


People also ask

Should I git ignore node_modules?

You should not include folder node_modules in your . gitignore file (or rather you should include folder node_modules in your source deployed to Heroku). If folder node_modules: exists then npm install will use those vendored libraries and will rebuild any binary dependencies with npm rebuild .

Does Nodejs have reusable packages?

Node modules allow you to write reusable code. You can nest them one inside another. Using the Node Package Manager (NPM), you can publish your modules and make them available to the community. Also, NPM enables you to reuse modules created by other developers.

Do I need to push node_modules in production?

Never push node_modules. So now that you understand that your dependencies are system specific and might include native modules, you should never assume that your node_modules folder will work in production.

How do I install all node modules at once?

It's simple. If you want to install all the node_modules from the package. json file you simply put: npm install in terminal (on the same directory where the package. json exists) and it would install all the node modules in the folder called node_modules .


1 Answers

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.

like image 111
topek Avatar answered Sep 22 '22 12:09

topek