Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way of adding a dependency to react in your package.json for a react component

I have made a few simple reusable react components and wanted to know the correct way to include a dependency to react in my package.json for publishing with npm.

I am currently doing this:

Assuming my component will use the most recent version of react and I have tested and it works with that version. e.g. 0.13.3

"peerDependencies": {    "react": "^0.13.3" }, 
like image 681
svnm Avatar asked May 26 '15 06:05

svnm


People also ask

How do I add dependencies to a package?

To add a package dependency to your Xcode project, select File > Swift Packages > Add Package Dependency and enter its repository URL.


1 Answers

For reusable components:

  1. Put a react dependency in both peerDependencies and devDependencies.
  2. Never put a react dependency in dependencies.

peerDependencies specifies which version(s) of React your reusable component supports/requires. When using npm 2 this also adds React to the list of modules to be installed, but this is no longer the case with npm 3.

devDependencies ensures React will be installed when you run npm install while developing your component, or when running tests on Travis or similar.

Putting react in dependencies will cause multiple versions of React to be installed if somebody uses your component but has a different version of React in their own package.json - having multiple versions of React not only bloats the build, but also causes errors when different versions try to interact.

like image 96
Jonny Buchanan Avatar answered Oct 11 '22 16:10

Jonny Buchanan