Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js and npm v3: How to add Travis tests compatible peerDependencies in package.json

Tags:

node.js

npm

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.

like image 330
user1984778 Avatar asked Nov 09 '15 21:11

user1984778


People also ask

What is peerDependencies in package json?

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.

Does npm 6 install peer dependencies?

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.

How do you handle peer dependencies when developing modules?

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.


2 Answers

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).

like image 170
Macil Avatar answered Nov 15 '22 12:11

Macil


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"
like image 26
stylesuxx Avatar answered Nov 15 '22 13:11

stylesuxx