Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

javascript - Question about dependency and peer dependency for my react app

Straight from the main Github repo for React Chartjs wrapper:

As of 2.x we have made chart.js a peer dependency for greater flexibility. Please add chart.js as a dependency on your project to use 2.x. Currently, 2.5.x is the recommended version of chart.js to use.

I npm installed the library into my React app, automatically both chart.js and react-chartjs-2 are now in my dependencies package.json file.

My question is based off of the quote above, do I now have to add the following field:

  "peerDependencies": {
    "chart.js": "^2.9.4",
    "react-chartjs-2": "^2.11.1"
  }

To my package.json file? Even if they are already both stated in my dependencies?

Thank you for clarifying.

question from:https://stackoverflow.com/questions/65836046/question-about-dependency-and-peer-dependency-for-my-react-app

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

No you don't have to. react-chartjs-2 exposes chart.js as peerDependency to expose to you its dependency.

This way if there is another chart.js incompatible version (it may come from another package that also depends on chart.js) npm will warn you during installation process about it, therefore you can handle the inconsistency properly.

If react-chartjs-2 haven't added it as peerDependecy, then you could face with different chart.js versions being installed, and it would may cause your application to crash.

To sum up, you would prefer to declare peerDependecies if you are building some plugin/library that it's expected to be consumed by others.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...