问题描述
我已经制作了一些简单的可重用的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"
},
推荐答案
对于可重用组件:
- 在
peerDependencies
和devDependencies
中都放置一个react
依赖项. - 从不将
react
依赖项放入dependencies
.
- Put a
react
dependency in bothpeerDependencies
anddevDependencies
. - Never put a
react
dependency independencies
.
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组件添加依赖项进行响应的正确方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!