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

ale*_*ngn 10 javascript node.js npm react-native npm-install

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

该组件依赖于另一个名为的npm包react-native-image-resizer.这个包需要链接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...
Run Code Online (Sandbox Code Playgroud)

另外,正如你在那里看到的那样,当我在我的示例项目中安装我的组件时,我有一个未满足的peer-dependencies问题,即使它在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"
  }
}
Run Code Online (Sandbox Code Playgroud)

知道为什么抱怨吗?

此处提供的组件回购:http://github.com/alexmngn/react-native-image-crop.git

谢谢

小智 3

rnpm link它找到的唯一链接包,package.json通常这些包是通过命令rnpm install或 来安装的npm install --save

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

在这样的package.json添加块中scripts

{
  "scripts": {
    "preinstall": "npm install --save react-native-image-resizer@0.0.9"
  }
}
Run Code Online (Sandbox Code Playgroud)

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

阅读有关npm 脚本的更多信息