dwyl / dwyl/learn-node-js-by-example

Can/should I use/require node_modules included by my dependencies' node_modules?

Open
#28 0 comments 0 reactions 0 assignees View on GitHub
help wanted question
Dominant language
HTML
Stars
48
Forks
11
PR merge metrics
No merged PRs in 30d

Description

**Scenario**:
_Imagine_ you are building a web application using a framework such as `hapi` and you _know_ that when you `npm install hapi --save` it installs several "_utilities_" in _it's_ [`node_modules`](https://github.com/hapijs/hapi/blob/master/package.json#L20) (_e.g_: [`boom`](https://www.npmjs.com/package/boom), [`joi`](https://www.npmjs.com/package/joi), [`hoek`](https://www.npmjs.com/package/hoek), _etc_.)

**Question**:
Can we avoid _explicitly re-installing_ `Joi` in the project and use the module included in **`my_app/node_modules/hapi/node_modules/joi/lib/`** e.g: in my `server.js` file:

``` js
var Hapi = require('hapi');
var Joi = require('./node_modules/hapi/node_modules/joi/lib/'); // good or bad idea? Why?

var server = new Hapi.Server();
server.connection({ port: 3000 });

server.route({
method: 'GET',
path: '/{name*}',
config: {
validate: { // validate using Joi
params: {
name: Joi.string().max(40).min(2).alphanum()
}
},
handler: function (req,reply) {
reply('Hello '+ req.params.name + '!');
}
}
});

server.start(function() {
console.log('Now Visit: http://localhost:3000/YOUR_NAME_HERE')
});
```

While the idea of not installing the same `node_modules` multiple times appears logical on the _surface_ we would like to know the _real_ world pitfalls of doing this? The clear advantage is that when you _update_ your dependency on `Hapi` you will also get all the latest versions of its' `dependencies` the potential _downside_ is that you won't know when there's a _breaking change_ in one of those `dependencies` ... what _else_?

> Note: I/we do not currently do this in any of our Node.js code, but someone asked me **_why not?**_ today and I did not have a _comprehensive answer for them..._
> "_Because it's bad practice..._" is not a good answer, we want to _understand_ **_why**_... not have the [_4 monkeys in a room_ "_reason_"](http://skeptics.stackexchange.com/questions/6828/was-the-experiment-with-five-monkeys-a-ladder-a-banana-and-a-water-spray-condu)

---

See: http://stackoverflow.com/questions/37237514/can-should-we-use-require-node-modules-included-by-my-dependencies-node-modules

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.