steven iseki steven iseki - 1 year ago 124
Node.js Question

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"

Answer Source

For reusable components:

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

peerDependencies specifies which version(s) of React your reusable component supports/requires. Currently this also adds React to the list of modules to be installed, but packages listed in peerDependencies will not be installed automatically as of npm v3.

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

Listing 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.