为什么 npm 没有安装来自 package.json 的 React?

Why is npm not installing react from package.json?

如果我删除我的 node_modules 并在我的 ReactNative 项目中进行干净的 npm 安装,我会收到警告 "react-native@0.37.0 requires a peer of react@~15.3.1 but none was installed." 但是,我已将反应列为 package.json 中的依赖项文件:

{
  "name": "MyApp",
  "version": "1.1.1",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start"
  },  
  "dependencies": {
    "lodash": "^4.17.2",
    "moment": "^2.16.0",
    "react": "^15.3.1",
    "react-native": "^0.37.0",
    "react-native-drawer": "^2.2.6",
    "react-native-htmlview": "^0.5.0",
    "react-native-keyboard-spacer": "^0.3.0",
    "react-native-material-design": "^0.3.7",
    "react-native-modal-picker": "0.0.16",
    "react-native-modalbox": "^1.3.4",
    "react-native-vector-icons": "^3.0.0",
    "react-native-viewpager": "^0.2.13",
    "rebound": "0.0.13"
  }
}

您的 react 依赖版本是 ^15.3.1。 semver 中的脱字符 ^ 允许版本 major.minor.patch 的次要范围内的任何版本。 NPM 目前将其解析为 15.4.2.

另一方面,React Native 中的 react 对等依赖是 ~15.3.1。波浪字符 ~ 只允许补丁版本内的变化,因此它与 15.4.2.

不兼容

定义你的反应依赖为~15.3.1,你会得到正确的版本。