繁体   English   中英

node.js 和 npm v3:如何在 package.json 中添加 Travis 测试兼容的 peerDependencies

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

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

模块_a

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

module_a 的 package.json

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

我的应用程序

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

文件结构

使用 npm v1/v2,此配置完美运行: npm install module_a module_a在根文件npm install module_a安装module_a和 module_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 不会自动安装对等依赖项,我必须为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可见。

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

谢谢你。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM