我刚刚为React-Native创建了一个组件,我将很快推送到npm作为包.虽然我正在面对一个问题.
该组件依赖于另一个名为react-native-image-resizer的npm软件包.这个包需要与rnpm相关联才能工作.
虽然,当我将组件安装在一个全新的项目中时,依赖关系将不会自动链接,并且本机库不会出现在项目中.当然,当我运行rnpm链接时,它也不会将其添加到项目中.
所以我想知道什么是最好的方式来安装和链接这个依赖关系?
MacBook-Pro:Example $npm install react-native-image-crop > react-native-image-crop@1.0.0 preinstall /Users/alexmngn/Work/react-native-image-crop/Example/node_modules/.staging/react-native-image-crop-95365d1b > npm install --save react-native-image-resizer react-native-image-crop@1.0.0 (git+ssh://git@github.com/alexmngn/react-native-image-crop.git#90e002c7d0f01c9d61277c30cad375560f09a94a) /Users/alexmngn/Work/react-native-image-crop/Example/node_modules/.staging/react-native-image-crop-95365d1b ├── UNMET DEPENDENCY react-native@^0.31.0 └── react-native-image-resizer@0.0.9 npm WARN react-native-image-resizer@0.0.9 requires a peer of react-native@>=v0.14.2 but none was installed. npm WARN react-native-image-crop@1.0.0 No repository field. - react-native-image-resizer@0.0.9 node_modules/react-native-image-crop/node_modules/react-native-image-resizer Example@0.0.1 /Users/alexmngn/Work/react-native-image-crop/Example └── react-native-image-crop@1.0.0 (git+ssh://git@github.com/alexmngn/react-native-image-crop.git#90e002c7d0f01c9d61277c30cad375560f09a94a) MacBook-Pro:Example $rnpm link MacBook-Pro:Example $# Nothing gets linked here...
另外,正如你可以看到的那样,当我在我的示例项目中安装我的组件时,我有一个未满足的同侪依赖关系问题,即使它在我的依赖关系中被正确列出(使用正确的版本)在package.json中:
{ "name": "Example","version": "0.0.1","private": true,"scripts": { "start": "node node_modules/react-native/local-cli/cli.js start" },"dependencies": { "react": "15.2.1","react-native": "^0.31.0","react-native-image-crop": "git+ssh://github.com/alexmngn/react-native-image-crop.git" } }
任何想法为什么抱怨?
可以在这里回收组件:http://github.com/alexmngn/react-native-image-crop.git
谢谢
解决方法
rnpm链接仅链接在package.json中找到的包,通常这些包通过命令rnpm install或npm install –save进行安装.
为了自动为那些安装软件包的人自动执行此操作,您可以编写一个预安装的npm脚本,该脚本将在程序包安装之前执行.
在thepackage.json中添加这样的脚本块
{ "scripts": { "preinstall": "npm install --save react-native-image-resizer@0.0.9" } }
这样做后,当有人尝试通过npm安装pacakge时,首先将安装react-native-image-resizer,并将include ab条目添加到package.json – >依赖关系使得rnpm链接可以正常工作.
阅读更多关于npm script的信息