问题标签 [peer-dependencies]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
0 回答
1174 浏览

javascript - es6中peerDependency的条件导入

我正在开发一个本地化日期(以及其他类型和对象)的 JavaScript i18n 库。

它当前依赖于moment.js,它被定义为 peerDependency (本地化是功能之一但不是唯一的,它可能不会被使用)

基本上类似于(更防错但我简化了逻辑):

问题是,如果moment.js它是一个好作品,它就像一个背包的石头,你不会把它带到 50 英里的小路上,特别是如果你只需要在整个应用程序中本地化一个日期。带宽方面,IMO 不值得(实际上在很多人看来也是如此)。

所以我正在考虑切换到更轻的库,例如date-fns,但我想出了一个我认为更好的选择:如果我们可以让另一个库选择最适合他的库怎么办?

我正在考虑定义与库相关的本地化程序的不同实现,并根据安装的 peerDependency 有条件地导入它们:

这在 JavaScript 中是否可行?

我认为“导入”语句必须作为文件中的第一条语句。也许require改用...(如ES6: Conditional & Dynamic Import Statements中所建议)?是否可以在不导入模块的情况下测试模块的存在(基本上如何编写该isModuleDefined()方法?)

我也看过那些:

node.js中的ES6变量导入名称?

如何有条件地导入 ES6 模块?

但是由于我们目前正在使用babel编译这个库,如果这个架构是可能的,它会不会在其他构建工具中造成编译问题,例如webpackgulpgrunt

0 投票
3 回答
44997 浏览

npm - 缺少对等依赖项

我是npmAngular 项目的新手,我bootstrap@4.1.1在我的package.json. 当我执行 npm install 时,我收到以下错误 -

这是否意味着我需要添加jquery@1.9.1 - 3到我的package.jsonpeerDependencies除了使用选项在本地安装它之外no-save部分?

另外,我们是否还需要在构建服务器上安装这个缺少的对等依赖项?还是可以忽略不计?

0 投票
2 回答
1970 浏览

node.js - 使用对等依赖和本地(文件:../some-lib)依赖

我有一个 monorepo,里面有很多微服务。我想让任何需要它的微服务都可以使用一些库类型的函数/类。但是,如果该库包声明了对等依赖项,则在从依赖于该库的事物中运行代码时将找不到对等依赖项。

考虑这个回购结构:

    • 一些库(peerDepends on foo
      • index.js(需要foo
      • node_modules 将为空
  • 服务
    • 一些服务(取决于foo, 和some-library
      • index.js(需要some-library
      • node_modules 将具有:
      • foo
      • some-library将是一个符号链接../../lib/some-library

运行时node services/some-service/index.js,您将收到错误“找不到模块 'foo'”,来自lib/some-library/index.js.

可能发生这种情况是因为节点只查看祖先目录中的lib/some-library/node_modules任何文件夹。node_modules但是由于此代码是从services/some-service(作为工作目录)运行的,并且由于 中的符号链接services/some-service/node_modules,我希望这可以工作。

这是一个您可以轻松克隆以查看问题的存储库:https ://github.com/jthomerson/example-local-dependency-problem

我只看到两个解决方案:

  • 不要在库中使用 peerDependencies
  • 为了本地开发和测试,在项目的根目录安装每个对等依赖项。

这些都不是一个真正好的解决方案,因为它不允许每个服务具有不同版本的依赖项,因此意味着如果依赖项的本地版本(或库的版本)被碰撞,所有使用该库的服务然后让它们的依赖版本同时碰撞,这使它们更加脆弱,因为它们都捆绑在一起。

0 投票
1 回答
1482 浏览

jquery - 如何使用 peerDependencies 解决 NPM 中的依赖地狱

设置:

鉴于以下几点

  • my_library对jquery进行了广泛的运行时使用。
  • my_library中,默认情况下通过npm所需的jquery(因为其中包含安全修复程序)。但是它也与jquery >=2.2.0兼容(但尚未在 中指定)^3.3.1package.json
  • my_library通过npmcustom_project中使用。
  • custom_project还需要outer_library,即使用不同且冲突的jquery版本(例如,假设jquery 1.7.3)。
  • custom_project_2只需要my_library in dependencies

问题:

  • 安装custom_project会引发重复的依赖关系,弄乱两个库之一的jquery 。
  • my_library中的jquery版本指定了一个建议的版本(为了避免严重的漏洞),但没有说明哪个jquery最低版本与my_library兼容 dependencies

最终解决方案:

为避免jquery依赖重复(outer_library 为 1.7.3,my_library3.3.1 )我可以将我的jquery ^3.3.1从to移动,因此我将在开发中获得3.3.1而不会在生产中安装( ) 并且只安装jquery 1.7.3dependenciesdevDependenciesnpm install --only=prod

但是这个:

  • 不保证my_library将获得兼容的jquery版本,因此my_library很容易中断。
    • jquery@>=2.2.0my_library 中添加peerDependencies至少会引发一个警告,要求在custom_project中手动安装特定版本来解决冲突(即使它可能无法解决)。
  • 对我来说感觉不对,因为jquery是一个运行时dependency并且不应该进入devDependencies(使用单元测试工具等)。实际上jquery不会安装在custom_project_2中,在生产上安装时(所以my_library会中断)

问题

  1. 我怎样才能满足my_library依赖的两个用例?

  2. (A) 如果outer_library需要与我的定义 ( )兼容的jquery ,我还需要手动安装jquery吗?还是npm会解析一个通用版本?peerDependencies>=2.0.0

  3. (B) 是否存在peerDependencies不抱怨且不需要手动安装任何东西的情况?(只要semvers受到尊重?)

  4. (A) 将jquery之类的依赖项(冲突的高概率)放在内部(尽可能松散的semver推荐版本的内部是否有意义?peerDependenciesdependencies

  5. (B) 在每个设置NPM版本<3(peerDependencies 自动安装)和>=3(需要手动安装)中都能正常工作吗?

如果您能回答部分问题,我们将不胜感激

0 投票
1 回答
40 浏览

javascript - How do I develop a plugin for a library when including it as a peerDependency gives me no concrete implementation to use?

The Requirements:

I am developing a plugin for popularLibrary.js. The plugin:

  • Will not work, at all, if popularLibrary.js is not present
  • Works with v1.x.x of popularLibrary.js
  • Must work if it's included as a dependency in a project that uses popularLibrary.js
  • Must work if it's included as a packaged source alongside popularLibrary.js

In example:

The Problems:

  • When I set popularLibrary.js as a peerDependency, it is no longer downloaded on npm install. How do I continue to develop my plugin when it needs to import and utilize functionality that exists in popularLibrary.js?
  • Not everyone uses a build step. If someone adds the minified sources for popularLibrary.js and myMagicalPlugin.js, how does that peerDependency resolve? Do I need to do anything extra/special in either library to support this scenario?
0 投票
0 回答
3046 浏览

npm - Should I bother resolving npm peer dependencies myself?

I seem to have gone down a rabbit hole with this. While I'm using webpack as an example, I'm looking for an answer that isn't specific to webpack. Ideally, I'd like to resolve these, but should I even bother?

I have an application whose dependencies have peer dependencies of various versions of webpack. When I run npm install, this is what I see (new lines added for clarity):

I tried to install all the various versions, but wasn't sure how. In my package.json, the entry for webpack just kept updating to the last installed. When I hit webpack@3.12.0, the number of dependencies went down to:

So, it seems that 3.12.0 meets the needs of most of my dependencies, but there are still some that are fussing. Ugh.

I tried putting things like "webpack": "^3.0.0 || ^3.0.0 || ^4.0.0" into my package.json but that didn't really do anything. Maybe I need to add them all?


I've looked at many of the answers here, but a lot of them are Angular specific or just say "upgrade to npm >= 3". Ideally, I'm looking for a way to get these warnings to go away in any situation where many dependencies require different versions of another dependency.

This answer seems to get really close: https://stackoverflow.com/a/22004559/2800116

I tried npm install --production, to no avail.

Many answers here ask how to fix this hell but are not yet answered: npm started requestion manual install of peer dependencies

My question is, should I even bother with getting these to resolve? It doesn't make the application break or anything, but it's just really annoying seeing these. I'm worried it'll create a "boy who cried wolf" situation where I'll just treat them as noise and miss things that could be important. Sometimes even adding one of these peers adds MORE unmet peer dependencies. Good grief!

0 投票
2 回答
3363 浏览

angular - ngrx/store@6.1.0 在升级到 Angular 7 时需要 @angular/core@^6.0.0 的同级

我尝试将 Angular 6 应用程序迁移到 Angular 7 应用程序并收到以下警告消息

这意味着什么 ?

这是否意味着 angular 7 没有兼容的商店?

我的商店不会工作吗?

0 投票
0 回答
482 浏览

npm - NPM 使用传递依赖解决对等依赖

我有 3 个包 A、B、C,我正在开发的包 A 有 B 和 C 作为依赖项,但这里的问题是包 C 也依赖于包 B,下面描述案例的块。

在此处输入图像描述

所以我想将包 B 作为 A、C 内部的对等依赖项,但是这样它不起作用,当我尝试对包 A 运行前单元测试时,它总是会失败

打包一个 package.json 文件

包 C 的 package.json 文件

除此之外,在包 C 中,我不会直接从包中导入 B 函数

而是我使用模块的绝对路径,例如: import X from 'C/X'

然后在 webpack 中我使用包 C 的别名,

对于本地开发,它可以正常工作,但是如果推送包并尝试将它们作为单个包(包 A)使用,它将无法工作,并且会出现上述 npm 错误。

我做得对,还是有其他方法可以处理这种情况?

0 投票
2 回答
6285 浏览

node.js - 如何使用 peerDependencies 测试 npm 模块?

我是新了解peerDependencies,并且我已阅读以下参考资料,以了解如何测试 npm 模块在其 package.json 中包含 peerDependencies:

但是,我还没有找到一个明确的解决方案来测试带有 peerDependencies 的 npm。有些人建议将 peerDependencies 添加为全局变量,有些人建议将 peerDependencies 包含在 devDependencies 中,但似乎都不对。

比如我有一个包,它有一个peer依赖,一个自定义的logger,这个logger需要被它的宿主包配置好才能使用。

这就是我使用这个 Gulp 任务执行大多数脚本测试的方式:

我确实收到了一个有用的建议(参见下面的评论,@estus)来使用npm-install-peers,但是,我还不确定它是否可以在使用前配置对等依赖项,因为它将由主机包执行。

非常感谢您的反馈和建议。

0 投票
1 回答
2782 浏览

node.js - 如何在将 Angular 应用程序从版本 6 升级到 7 后修复对等依赖警告

我已经购买了一个 Angular 6 应用程序的管理模板(来自 wrappixel)。我需要将其升级到 Angular 版本 7。我这样做是使用

ng 更新 --force --all

注意,我必须使用 --force 选项来确保更新通过,目的是在更新后修复对等依赖项

现在需要修复以下对等依赖项,因为现在构建已损坏:

阅读了有关对等依赖项的各种文章(包括对等依赖项,其中谈到了很多关于插件),我仍然不知道如何解释这些警告背后的含义或如何修复它们。

所以让我们采取第一个:

ng2-smart-table@1.3.5 需要 ng2-completer@^2.0.8 的 peer

这是什么意思?

我的 package.json 包含(在依赖项部分):

ng2-completer 不在我的 package.json 中,因为它不是直接依赖项。

这个错误是不是因为它目前没有被指定为直接依赖,它应该是;即我应该只安装所需的版本,使其成为直接依赖项吗?(npm install ng2-completer@^2.0.8 ??)

我在其他文章中看到有一个“peerDependencies”可以进入 package.json,这是我自己从未见过的。那么我应该创建这个部分并在其中添加“ng2-completer@^2.0.8”吗?

如果不修复这些对等依赖项,当我去构建应用程序时,我会收到以下错误:

多个 css 文件中存在多重错误,找不到模块错误和“类型上不存在属性'订阅'”错误。我不知道它们是否与对等依赖警告有关,但我想它们是。

在我的 angular.json 中,我的“样式”部分包括:

并且这些 css 文件与看到的“多错误”错误消息相匹配。我发现另一篇文章提到“src/styles.css”应该更改为“styles.css”,我试过了,但这没有任何区别。

还:

看起来 ng2-smart-table@1.3.5 依赖于 Angular 6,但我刚刚将项目升级到 Angular 7。“ng2-smart-table@1.3.5”也是问题,依赖关系需要改变到适用于 Angular 7 的更高版本?

我希望有人可以帮助我修复对等依赖警告,最终目的是修复所有这些构建错误。

谢谢。