自定义节点模块的依赖项未安装在 root 上

custom node module's dependency not installed on root

我有一个应用程序项目和 2 个节点模块项目。

依赖结构是这样的:

App {
  NodeModule1 {
    NodeModule2,
    ...
  },
  ...
}

我遇到的问题是我的 NodeModule2 没有安装在应用 node_module App/node_modules/NodeModule2 的根目录中,而是安装在 App/node_modules/NodeModule1/node_modules/NodeModule2

这导致了一些运行时错误,说找不到我的 NodeModule2。我的解决方法是直接将NodeModule2添加到App中,这不是一个好的解决方案。

NodeModule1 的所有其他依赖项都按预期安装在 App/node_modules/..。

我的 NodeModule2 的 package.json

{
  "name": "NodeModule2",
  "version": "0.0.2-20210202.1.0",
  "private": false,
  "description": "",
  "author": "",
  "license": "ISC",
  "peerDependencies": {
    "react": "16.13.1",
    "react-native": "0.59.10",
    ...
  }
}

我知道了

为了使子 node_module 出现在根级别,其所有 peerDependencies 的版本必须与 App 的版本相匹配。

在我的例子中,我的 App 和 NodeModule1 都有 dependencyreact-native-picker-select,但版本不同。

App {
  dependency: {
    "react-native-picker-select": "^8.0.0"
  }
}

NodeModule1 {
  dependency: {
    "react-native-picker-select": "8.0.0"
  }
}

NodeModule2 {
  peerDependency: {
    "react-native-picker-select": "8.0.0"
  }
}

在这种情况下,App 收到 8.0.4,NodeModule1 收到 8.0.0。 Yarn 将 NodeModule2 置于 NodeModule1 下并共享相同的依赖版本 8.0.0.

修复:确保 App、NodeModule1 和 NodeModule2 中的所有版本都匹配。