If I need to require an npm module that is already installed as a sub dependency to another module, should I still install it with npm?
For instance, kue requires redis so red is is installed with kue e.g.
npm install kue
/node_modules/kue/node_modules/redis
should I still install redis as well, thus npm install redis
/node_modules/redis/
or is there a way I can add a reference to the redis installed from kue in my require statements
require("/node_modules/kue/node_modules/redis")
instead of
require ("redis")
or is there a better way?
Here’s a general rule of thumb:
If you
require('module-name')in your application’s source code, you should listmodule-nameas a dependency. Treat dependencies of dependencies as opaque implementation details, and don’t concern yourself with them.This allows kue to depend on a different redis module version than your application, or switch out its implementation for a different module entirely. All without affecting your application.
Node’s module system is different than “conventional” one (e.g. Ruby), in that multiple versions of the same module can be loaded in the same process without causing a conflict. This feels a bit off at first, but just go with it and you’ll see benefits quickly. I suspect that as the community explores this aspect of Node further, we’ll see some interesting projects arise to take advantage of it.