在我的例子中,我使用 monorepo 与单个 repo 中的多个包。我解决了这个错误
- 删除
packages/myapp/node_modules
- 从项目根目录运行
yarn install。不在包裹内
- 已解决。现在可以运行
yarn ios 或yarn android 命令
注意:
对于自动链接库,您需要将根 package.json 文件中的所有依赖项也复制到 packages/myapp/package.json 文件中。见https://github.com/react-native-community/cli/blob/master/docs/autolinking.md#how-can-i-use-autolinking-in-a-monorepo
例子:
"dependencies": {
"@react-native-community/datetimepicker": "^3.5.2",
"@react-native-community/masked-view": "^0.1.11",
"@react-navigation/native": "^5.9.4",
"@react-navigation/stack": "^5.14.5",
"@reduxjs/toolkit": "^1.6.0",
"@voximplant/react-native-foreground-service": "^2.0.0",
"axios": "^0.21.1",
"babel-eslint": "^10.0.3",
"eslint-config-airbnb": "^18.0.1",
"eslint-plugin-import": "^2.18.2",
...
...
但请确保不要复制
"react-native-cli": "^any version",
否则react-native-cli 会发生冲突,您将继续看到类似的错误
Command `run-android` unrecognized. Make sure that you have run `npm install` and that you are inside a react-native project.
error Command failed with exit code 1.
或
Command `run-ios` unrecognized. Make sure that you have run `npm install` and that you are inside a react-native project.
error Command failed with exit code 1.