React Native组件依赖项,需要rnpm链接

时间:2016-09-02 02:57:51

标签: javascript node.js npm react-native npm-install

我刚刚为React-Native创建了一个组件,我将很快推送到npm作为一个包。虽然我遇到了一个问题。

该组件依赖于另一个名为react-native-image-resizer的npm包。此程序包需要与rnpm链接才能正常工作。

虽然,当我在一个全新的项目中安装我的组件时,依赖项将不会自动链接,并且本机库不会出现在项目中。当然,当我运行rnpm link时,它也不会将它添加到项目中。

所以我想知道安装和链接此依赖项的最佳方法是什么?

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

另外,正如你可以看到的那样,当我在我的示例项目中安装我的组件时,我有一个未满足的peer-dependent问题,即使它在我的依赖包中正确列出(使用正确的版本)上传.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

由于

1 个答案:

答案 0 :(得分:3)

rnpm link仅链接在package.json中找到的包,通常这些包是通过命令rnpm installnpm install --save安装的。

为了自动为安装软件包的人执行此操作,您可以编写一个preinstall npm脚本,该脚本将在安装软件包之前执行。

package.json添加scripts块中,像这样

{
  "scripts": {
    "preinstall": "npm install --save react-native-image-resizer@0.0.9"
  }
}

执行此操作后,当有人尝试通过npm安装您的pacakge时,将首先安装react-native-image-resizer,并将package ab条目添加到package.json - >依赖性,以便rnpm链接可以正常工作。

阅读有关npm script

的更多信息
相关问题