在package.json中为react组件添加依赖项进行响应的正确方法是什么 [英] What is the correct way of adding a dependency to react in your package.json for a react component

查看:384
本文介绍了在package.json中为react组件添加依赖项进行响应的正确方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经制作了一些简单的可重用的react组件,并想知道在我的 package.json 中包含依赖项以进行npm发布的正确方法.

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.

我目前正在这样做:

假设我的组件将使用react的最新版本并且我已经测试过,并且可以使用该版本.例如0.13.3

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"
},

推荐答案

对于可重用组件:

  1. peerDependenciesdevDependencies中都放置一个react依赖项.
  2. 从不react依赖项放入dependencies.
  1. Put a react dependency in both peerDependencies and devDependencies.
  2. Never put a react dependency in dependencies.


peerDependencies指定您的可重用组件支持/需要哪个版本的React.使用npm 2时,这还会将React添加到要安装的模块列表中,但是npm 3不再是这种情况.


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确保在开发组件时运行npm install或在Travis或类似设备上运行测试时安装React.

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

如果有人使用您的组件但在自己的package.json中具有不同版本的React,则将react放入dependencies会导致安装多个版本的React –具有多个版本的React不仅会使构建the肿,但是当不同版本尝试进行交互时也会导致错误.

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.

这篇关于在package.json中为react组件添加依赖项进行响应的正确方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆