【问题标题】:node.js and npm v3: How to add Travis tests compatible peerDependencies in package.jsonnode.js 和 npm v3:如何在 package.json 中添加 Travis 测试兼容 peerDependencies
【发布时间】:2015-11-09 21:55:08
【问题描述】:

使用 package.json 中的 peerDependencies 我可以确保用户在应用程序根文件夹中有一个特定的模块:

module_a

var modB = require('module_b')
...

module_a 的package.json

...
"peerDependencies": {
  "module_b": "^1.0.1"
},
"dependencies": {
},
...

my_app

var modA = require('module_a')
var modB = require('module_b')
...

文件结构

使用 npm v1/v2,此配置完美运行:npm install module_a 在根文件夹中安装 module_amodule_b

my_app
  node_modules
    module_a
    module_b

太好了,这就是我想要的!

npm v3

但是在使用npm install module_a 安装时,npm v2 会打印此警告:

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.

所以,npm v3 不会自动安装 peer 依赖项,我必须手动安装它以便 my_app 达到相同的结果。

问题

但是在我使用 npm v3 的测试中呢?

npm v3 没有安装 peer 依赖,所以 travis 会因为找不到 module_b 而失败。但是我不能将模块添加为常规依赖项,因为 my_appmodule_a 使用 module_b 的不同“实例”:

my_app
  node_modules
    module_a
      node_modules
        module_b         // used by module_a
    module_b             // used by my_app

这不是我想要的,因为 module_a 更改了 module_b 中的一些参数,但这些更改在 my_app 中不可见。 p>

问题

如何在不破坏 module_a 的 travis 测试的情况下在根文件夹中添加 module_b 作为(对等)依赖项?

谢谢。

【问题讨论】:

    标签: node.js npm


    【解决方案1】:

    如果一个模块不在依赖项列表中但需要运行测试,那么您应该将其列为 devDependency(无论它是否也是 peerDependency)。

    【讨论】:

      【解决方案2】:

      我通过在 travis 的 before_install 挂钩中安装对等依赖项解决了这个问题。
      我的 .travis.yml 看起来像这样:

      language: node_js
      node_js:
        - "6.1"
        - "5.11"
        - "4.4"
      before_install:
        - "npm install peerDependency"
      

      【讨论】:

        猜你喜欢
        • 2019-06-12
        • 2020-01-19
        • 2017-08-24
        • 2021-10-04
        • 1970-01-01
        • 2015-12-08
        • 1970-01-01
        • 1970-01-01
        • 2020-05-16
        相关资源
        最近更新 更多