首页 web前端 js教程 专业开发人员的 npm 对等依赖关系

专业开发人员的 npm 对等依赖关系

Oct 14, 2024 pm 01:27 PM

npm Peer Dependencies for the Professional Developer

在本文中,我将阐明 npm 对等依赖项 是什么,特别是何时应该使用它们。 对等依赖项 列在项目的 package.json 文件的peerDependency 对象中。

要充分利用本文,您至少应该对 npm 有一个介绍性的了解。

内容

在这篇文章中:

  1. 我们将比较对等依赖关系与常规依赖关系的工作方式。
  2. 我们将查看一些关于对等依赖关系和依赖关系的示例
  3. 然后,我们将研究 npm 如何处理版本冲突
  4. 最后,在我们牢牢掌握了基础知识后,我们将提出一种方法来决定何时适当的对等依赖关系

场景

为了保持真实,我们假设您正在创建一个 Angular 或 React 组件库,甚至只是一个导出一些函数的简单 JavaScript 文件。

您的项目依赖于 npm 注册表中的包。这些包是您项目的依赖项

您想从您的项目创建自己的npm 包。因此,您使用 npm pack 从您的项目生成 npm 包。您甚至可能决定将其发布到 npm 注册表。

其他团队可以在 npm 注册表上找到您的组件库作为​​。他们可以使用 npm install 将您的包添加为他们自己的项目中的依赖项。我们在 package.json 中使用依赖项对等依赖项来告诉其他项目还需要添加哪些包才能使我们的组件库正常工作。

依赖关系与对等依赖关系

在最基本的层面上,这是依赖关系对等依赖关系的工作原理:

依赖关系

依赖项列在项目的 package.json 文件的依赖项对象中。

当您在代码的依赖项中添加包时,您是在说:

  • 我的代码需要这个包才能运行。
  • 如果我的node_modules目录中尚不存在此包,则自动添加它。
  • 此外,添加此包的依赖项中列出的任何包。这些包称为传递依赖项

对等依赖性

对等依赖项列在项目的 package.json 文件的peerDependency 对象中。

通过在代码的对等依赖项中添加一个包,您可以说:

  • 我的代码与此版本的软件包兼容。
  • 如果该包已存在于node_modules中,则不执行任何操作。
  • 如果node_modules目录中尚不存在此包或者版本错误,请不要添加它。但是,向用户显示未找到它的警告。

添加依赖项

因此,我们在 npm 包文件夹的 package.json 文件中添加依赖项。让我们具体看看如何添加包作为依赖项以及包依赖项的一些示例。

添加依赖项

A 依赖项 是一个 npm 包,我们的代码依赖它才能运行。一些可以作为依赖项添加的流行软件包是 lodash、D3 和 Chartjs。

我们添加如下常规依赖项:

npm install lodash
登录后复制

npm 将包名称和版本添加到我们项目的 package.json 文件中的依赖项对象。

"dependencies": {
  "lodash": "^4.17.11"
}
登录后复制

你们中的一些人可能还记得过去我们必须使用 --save 标志来让 npm 更新 package.json 中的依赖项。值得庆幸的是,我们不需要再这样做了。

添加对等依赖项

对等依赖项 用于指定我们的项目与 npm 包的特定版本兼容。 Angular 和 React 就是很好的例子。

要添加对等依赖,您实际上需要手动修改package.json 文件。例如,对于组件库项目,根据您使用的框架,我建议添加 angular/corereact 作为对等依赖项。

因此,如果您想指定您的包是为 React 18 构建的,您可以包含如下内容:

"peerDependencies": {
   "react": "^18.0.0",
}
登录后复制

或者你可能想说你已经使用 Angular 版本 17 和 18 测试了你的组件库,但没有使用 19,因为它还没有发布。然后你可以使用:

"peerDependencies": {
   "@angular/core": ">=17.0.0 || <19"
}
登录后复制

About Conflicts

I get a lot of questions about whether a certain npm package should go into dependencies or into peerDependencies. The key to making this decision involves understanding how npm deals with version conflicts.

If you have read my previous articles, you know I like you to be able to do this stuff along with me! So feel free to work along with me for this little npm experiment.

conflict-test Project

To get started let’s create a trivial test project. I am going to name mine:
conflict-test

I created it like this:

md conflict-test
cd conflict-test
npm init -y
登录后复制

I then manually edited the package.json file and added two dependencies:

"dependencies": {
    "todd-a": "^1.0.0",
    "todd-b": "^1.0.0"
}
登录后复制

These todd-a and todd-b packages also have their own dependencies:

todd-a

"dependencies": {
    "lodash": "^4.17.11",
    "todd-child": "^1.0.0"
}
登录后复制

todd-b

"dependencies": {
    "lodash": "^4.17.11",
    "todd-child": "^2.0.0"
}
登录后复制

The thing I want you to notice here is that todd-a and todd-b use the same version of lodash. But, they have a version conflict for todd-child:
todd-a uses todd-child version 1.0.0
todd-b uses todd-child version 2.0.0

Now I know that, like me, you are keenly interested in seeing how npm handles this version conflict. In my main project conflict-test I run npm install. As we would expect, npm magically installs the todd-a and todd-b packages in our node_modules folder. It also adds the packages that they depend on (the transitive dependencies). So after running npm install we take a look at the node_modules folder. It looks like this:

node_modules
├── lodash 4.17.11
├── todd-a 1.0.0
├── todd-b 1.0.0
│   └── node_modules
│       └── todd-child 2.0.0
└── todd-child 1.0.0
登录后复制

The interesting thing about this is that our project has one copy of lodash. But, it has two copies of todd-child! Notice that todd-b gets its own private copy of todd-child 2.0.0.

So here is the rule:

npm deals with version conflicts by adding duplicate private versions of the conflicted package.

An Approach to Peer Dependencies

As we saw from our experiment with npm version conflicts, if you add a package to your dependencies, there is a chance it may end up being duplicated in node_modules.

Sometimes, having two versions of the same package is fine. However, some packages will cause conflicts when there are two different versions of them in the same code base.

For example, assume our component library was created using React v15. We wouldn’t want our package adding another completely different version of react when someone adds it as a dependency to their React v18 application.

The key is:
We don’t want our library adding another version of a package to node-modules when that package could conflict with an existing version and cause problems.

peerDependencies or dependencies?

So this brings us to the main question for our dependencies:

When my package depends on another package, should I put it in dependencies or peerDependencies?

Well, as with most technical questions: It depends.

Peer Dependencies express compatibility. For example, you will want to be specific about which version of Angular or React your library is compatible with.

The Guidelines

Favor using Peer Dependencies when one of the following is true:

  • Having multiple copies of a package would cause conflicts
  • The dependency is visible in your interface
  • You want the developer to decide which version to install

Let’s take the example of angular/core. Obviously, if you are creating an Angular Library, angular/core is going to be a very visible part of your library’s interface. Hence, it belongs in your peerDependencies.

However, maybe your library uses Moment.js internally to process some time related inputs. Moment.js most likely won’t be exposed in the interface of your Angular or React components. Hence, it belongs in your dependencies.

Angular or React as a Dependency

Given that you are going to specify in your documentation that your library is a set of Angular or React Components, you may be asking the question:

Do I even need to specify angular/core as a dependency? If someone is using my library, they will already have an existing Angular project.

Good question!

Yes, we can usually assume that for our Angular or React specific library the Workspace will already have the Angular or React packages available. Hence, technically we wouldn’t need to bother adding them to our list of dependencies.

但是,我们确实想告诉开发人员我们的库与哪些 Angular 或 React 版本兼容。所以我推荐以下方法:

至少将兼容版本的 Angular/core 或 React 包添加到你的peerDependency中。

这样,如果开发人员尝试在其 React 16 项目中使用您的 React 18 组件库,他们将看到警告。不必费心添加其他 Angular 或 React 包。你可以假设如果他们有 Angular/Core 或 React,他们就有其他相关的库。

综上所述

当有疑问时,您可能应该倾向于使用peerDependency。这可以让您的包的用户自行选择要添加的包。

以上是专业开发人员的 npm 对等依赖关系的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

<🎜>:泡泡胶模拟器无穷大 - 如何获取和使用皇家钥匙
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系统,解释
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆树的耳语 - 如何解锁抓钩
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Java教程
1670
14
CakePHP 教程
1428
52
Laravel 教程
1329
25
PHP教程
1274
29
C# 教程
1256
24
Python vs. JavaScript:学习曲线和易用性 Python vs. JavaScript:学习曲线和易用性 Apr 16, 2025 am 12:12 AM

Python更适合初学者,学习曲线平缓,语法简洁;JavaScript适合前端开发,学习曲线较陡,语法灵活。1.Python语法直观,适用于数据科学和后端开发。2.JavaScript灵活,广泛用于前端和服务器端编程。

从C/C到JavaScript:所有工作方式 从C/C到JavaScript:所有工作方式 Apr 14, 2025 am 12:05 AM

从C/C 转向JavaScript需要适应动态类型、垃圾回收和异步编程等特点。1)C/C 是静态类型语言,需手动管理内存,而JavaScript是动态类型,垃圾回收自动处理。2)C/C 需编译成机器码,JavaScript则为解释型语言。3)JavaScript引入闭包、原型链和Promise等概念,增强了灵活性和异步编程能力。

JavaScript和Web:核心功能和用例 JavaScript和Web:核心功能和用例 Apr 18, 2025 am 12:19 AM

JavaScript在Web开发中的主要用途包括客户端交互、表单验证和异步通信。1)通过DOM操作实现动态内容更新和用户交互;2)在用户提交数据前进行客户端验证,提高用户体验;3)通过AJAX技术实现与服务器的无刷新通信。

JavaScript在行动中:现实世界中的示例和项目 JavaScript在行动中:现实世界中的示例和项目 Apr 19, 2025 am 12:13 AM

JavaScript在现实世界中的应用包括前端和后端开发。1)通过构建TODO列表应用展示前端应用,涉及DOM操作和事件处理。2)通过Node.js和Express构建RESTfulAPI展示后端应用。

了解JavaScript引擎:实施详细信息 了解JavaScript引擎:实施详细信息 Apr 17, 2025 am 12:05 AM

理解JavaScript引擎内部工作原理对开发者重要,因为它能帮助编写更高效的代码并理解性能瓶颈和优化策略。1)引擎的工作流程包括解析、编译和执行三个阶段;2)执行过程中,引擎会进行动态优化,如内联缓存和隐藏类;3)最佳实践包括避免全局变量、优化循环、使用const和let,以及避免过度使用闭包。

Python vs. JavaScript:社区,图书馆和资源 Python vs. JavaScript:社区,图书馆和资源 Apr 15, 2025 am 12:16 AM

Python和JavaScript在社区、库和资源方面的对比各有优劣。1)Python社区友好,适合初学者,但前端开发资源不如JavaScript丰富。2)Python在数据科学和机器学习库方面强大,JavaScript则在前端开发库和框架上更胜一筹。3)两者的学习资源都丰富,但Python适合从官方文档开始,JavaScript则以MDNWebDocs为佳。选择应基于项目需求和个人兴趣。

Python vs. JavaScript:开发环境和工具 Python vs. JavaScript:开发环境和工具 Apr 26, 2025 am 12:09 AM

Python和JavaScript在开发环境上的选择都很重要。1)Python的开发环境包括PyCharm、JupyterNotebook和Anaconda,适合数据科学和快速原型开发。2)JavaScript的开发环境包括Node.js、VSCode和Webpack,适用于前端和后端开发。根据项目需求选择合适的工具可以提高开发效率和项目成功率。

C/C在JavaScript口译员和编译器中的作用 C/C在JavaScript口译员和编译器中的作用 Apr 20, 2025 am 12:01 AM

C和C 在JavaScript引擎中扮演了至关重要的角色,主要用于实现解释器和JIT编译器。 1)C 用于解析JavaScript源码并生成抽象语法树。 2)C 负责生成和执行字节码。 3)C 实现JIT编译器,在运行时优化和编译热点代码,显着提高JavaScript的执行效率。

See all articles