With peerDependencies in package.json I can make sure that the user has a specific module in the application rootfolder:
module_a
var modB = require('module_b')
...
package.json of module_a
...
"peerDependencies": {
"module_b": "^1.0.1"
},
"dependencies": {
},
...
my_app
var modA = require('module_a')
var modB = require('module_b')
...
file structure
With npm v1/v2 this configuration works perfect: npm install module_a
installs module_a and module_b in the rootfolder:
my_app
node_modules
module_a
module_b
Great, that is what I want!
npm v3
But while installing with npm install module_a
, npm v2 prints this warning:
npm WARN peerDependencies The peer dependency module_b@^1.0.1 included
from module_a will no longer be automatically installed to fulfill the
peerDependency in npm 3+. Your application will need to depend on it
explicitly.
So, npm v3 will not install the peer dependency automatically, I have to install it manually for my_app to achieve the same result.
The problem
But what about in my tests when using npm v3?
npm v3 doesn't install the peer dependency, so travis will fail because it cannot find module_b. But I cannot add the module as regular dependency because than my_app and module_a use different "instances" of module_b:
my_app
node_modules
module_a
node_modules
module_b // used by module_a
module_b // used by my_app
That is not what I want because module_a changes some parameters in module_b, but this changes aren't visible in my_app.
Question
How can I add module_b as (peer) dependency in the rootfolder without breaking the travis tests from module_a?
Thank you.
Peer Dependencies: In package. json file, there is an object called as peerDependencies and it consists of all the packages that are exactly required in the project or to the person who is downloading and the version numbers should also be the same. That is the reason they were named as peerDependencies.
With npm version 4 through to 6, a warning is issued when you run npm install to remind you to install the peer dependencies. Prior to version 4, npm automatically included peer dependencies if they weren't explicitly included.
Ask user to install a dependency your module needs to work without specifying a version in particular. Prevents having multiple version of a same module in user's app node_modules. Reduce javascript files size to load on browser side particularly useful for mobile users.
If a module is not in the dependencies list but is required to run the tests, then you should list it as a devDependency (regardless of whether it's also a peerDependency).
I solved this by installing the peer dependencies in travis' before_install hook.
My .travis.yml looks like so:
language: node_js
node_js:
- "6.1"
- "5.11"
- "4.4"
before_install:
- "npm install peerDependency"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With