この記事では、npm ピア依存関係 とは何か、特にそれらをいつ使用する必要があるかを明確にします。 ピア依存関係 は、プロジェクトの package.json ファイルのpeerDependency オブジェクトにリストされています。
この記事を最大限に活用するには、少なくとも npm の入門知識を持っている必要があります。
コンテンツ
この記事の内容:
- ピア依存関係と通常の依存関係がどのように機能するかを正確に比較します。
- ピア依存関係と依存関係の両方の例を見ていきます。
- 次に、npm が バージョンの競合 をどのように処理するかを調べます。
- 最後に、基本をしっかりと理解したので、いつピア依存関係が適切であるかを決定するためのアプローチを示します。
シナリオ
現実を保つために、Angular または React コンポーネント ライブラリ、あるいはいくつかの関数をエクスポートする単純な JavaScript ファイルを作成していると仮定しましょう。
あなたのプロジェクトは、npm レジストリのパッケージに依存しています。これらのパッケージはプロジェクトの依存関係です。
プロジェクトから独自の npm パッケージ を作成したいと考えています。したがって、npm Pack を使用して、プロジェクトから npm パッケージ を生成します。 npm レジストリに公開することもできます。
他のチームは、npm レジストリ上の パッケージ としてコンポーネント ライブラリを見つけることができます。 npm install を使用して、パッケージを依存関係として自分のプロジェクトに追加できます。 package.json で Dependency と Peer dependency を使用して、コンポーネント ライブラリが機能するためにどのパッケージも追加する必要があるかを他のプロジェクトに伝えます。
依存関係とピア依存関係
最も基本的なレベルで、依存関係 と ピア依存関係 がどのように機能するかを示します。
依存関係
依存関係は、プロジェクトの package.json ファイルの依存関係オブジェクトにリストされます。
コードの依存関係にパッケージを追加すると、次のようになります:
- 私のコードを実行するにはこのパッケージが必要です。
- このパッケージが node_modules ディレクトリに存在しない場合は、自動的に追加されます。
- さらに、このパッケージの依存関係にリストされているパッケージを追加します。これらのパッケージは 推移的な依存関係 と呼ばれます。
ピアの依存関係
ピアの依存関係は、プロジェクトの package.json ファイルのpeerDependency オブジェクトにリストされます。
コードのpeerDependencyにパッケージを追加すると、次のようになります。
- 私のコードは、このバージョンのパッケージと互換性があります。
- このパッケージが既に node_modules に存在する場合は、何もしません。
- このパッケージがnode_modulesディレクトリに存在しないか、バージョンが間違っている場合は、追加しないでください。ただし、見つからなかったことをユーザーに警告します。
依存関係の追加
そこで、npm パッケージ フォルダーの package.json ファイルに依存関係を追加します。パッケージを依存関係として追加する正確な方法と、パッケージの依存関係の例をいくつか見てみましょう。
依存関係の追加
依存関係 は、コードが実行できるようにするために依存する npm パッケージです。依存関係として追加できる一般的なパッケージには、lodash、D3、chartjs などがあります。
次のような通常の依存関係を追加します。
npm install lodash
npm は、プロジェクトの package.json ファイル内の依存関係オブジェクトにパッケージ名とバージョンを追加します。
"dependencies": { "lodash": "^4.17.11" }
package.json 内の依存関係を更新するために npm を取得するために --save フラグを使用する必要があった昔のことを覚えている人もいるかもしれません。ありがたいことに、もうその必要はありません。
ピア依存関係の追加
ピア依存関係 は、プロジェクトが npm パッケージの特定のバージョンと互換性があることを指定するために使用されます。良い例は Angular と React です。
ピア依存関係を追加するには、実際にはpackage.jsonファイルを手動で変更する必要があります。たとえば、コンポーネント ライブラリ プロジェクトの場合、使用しているフレームワークに応じて、angular/core または react をピア依存関係として追加することをお勧めします。
パッケージが React 18 用にビルドされていることを指定したい場合は、次のようなものを含めることができます:
"peerDependencies": { "react": "^18.0.0", }
あるいは、Angular バージョン 17 と 18 の両方でコンポーネント ライブラリをテストしたが、19 はまだリリースされていなかったためテストしなかったと言いたいかもしれません。次に、以下を使用できます:
"peerDependencies": { "@angular/core": ">=17.0.0 || <h2> About Conflicts </h2> <p>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.</p> <p>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.</p> <h3> conflict-test Project </h3> <p>To get started let’s create a trivial test project. I am going to name mine:<br> conflict-test</p> <p>I created it like this:<br> </p> <pre class="brush:php;toolbar:false">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中文網其他相關文章!

JavaScript框架的強大之處在於簡化開發、提升用戶體驗和應用性能。選擇框架時應考慮:1.項目規模和復雜度,2.團隊經驗,3.生態系統和社區支持。

引言我知道你可能會覺得奇怪,JavaScript、C 和瀏覽器之間到底有什麼關係?它們之間看似毫無關聯,但實際上,它們在現代網絡開發中扮演著非常重要的角色。今天我們就來深入探討一下這三者之間的緊密聯繫。通過這篇文章,你將了解到JavaScript如何在瀏覽器中運行,C 在瀏覽器引擎中的作用,以及它們如何共同推動網頁的渲染和交互。 JavaScript與瀏覽器的關係我們都知道,JavaScript是前端開發的核心語言,它直接在瀏覽器中運行,讓網頁變得生動有趣。你是否曾經想過,為什麼JavaScr

Node.js擅長於高效I/O,這在很大程度上要歸功於流。 流媒體匯總處理數據,避免內存過載 - 大型文件,網絡任務和實時應用程序的理想。將流與打字稿的類型安全結合起來創建POWE

Python和JavaScript在性能和效率方面的差異主要體現在:1)Python作為解釋型語言,運行速度較慢,但開發效率高,適合快速原型開發;2)JavaScript在瀏覽器中受限於單線程,但在Node.js中可利用多線程和異步I/O提升性能,兩者在實際項目中各有優勢。

JavaScript起源於1995年,由布蘭登·艾克創造,實現語言為C語言。 1.C語言為JavaScript提供了高性能和系統級編程能力。 2.JavaScript的內存管理和性能優化依賴於C語言。 3.C語言的跨平台特性幫助JavaScript在不同操作系統上高效運行。

JavaScript在瀏覽器和Node.js環境中運行,依賴JavaScript引擎解析和執行代碼。 1)解析階段生成抽象語法樹(AST);2)編譯階段將AST轉換為字節碼或機器碼;3)執行階段執行編譯後的代碼。

Python和JavaScript的未來趨勢包括:1.Python將鞏固在科學計算和AI領域的地位,2.JavaScript將推動Web技術發展,3.跨平台開發將成為熱門,4.性能優化將是重點。兩者都將繼續在各自領域擴展應用場景,並在性能上有更多突破。

Python和JavaScript在開發環境上的選擇都很重要。 1)Python的開發環境包括PyCharm、JupyterNotebook和Anaconda,適合數據科學和快速原型開發。 2)JavaScript的開發環境包括Node.js、VSCode和Webpack,適用於前端和後端開發。根據項目需求選擇合適的工具可以提高開發效率和項目成功率。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

Atom編輯器mac版下載
最受歡迎的的開源編輯器

SublimeText3漢化版
中文版,非常好用