初次尝试RN,结果搞死我了,在M1芯片下安装环境,各种不明所以的问题。找了各种解决方案,最后还是自己瞎搞出来了…
首先我是按照官网的操作配置的,搭建开发环境,到init项目的时候 报错了。
-
node
node我是用nvm安装了nodev18
-
watchman
brew install watchman
-
npm源
npx nrm use taobao
-
yarn
npm install -g yarn
其实之前我已经安装过,毕竟yarn很好用。 -
Xcode
每个苹果开发者都要安装的App,之前开发过swift,所以没有安装的朋友自行 安装就可以了。至于官网提到的Xcode的相关配置,照做就好了,不过在下方官网提到的,新版Xcode已经将Preferences改成setting…,在这里面找就行。在 Xcode 中安装 iOS 模拟器,安装模拟器只需打开 Xcode > Preferences… 菜单
-
Cocoapods
这个搞了我好长时间,一来是按照官网傻瓜式操作,也没有说验证安装成功的命令,二来很久不写swift,记错了验证命令行。我采用了
brew install cocoapods
安装完要
pod -v
验证是否安装成功 -
初始化项目
npx react-native@latest init AwesomeProject
这是官网的命令,照做了…
重点来了…
我收到了以下的报错
✔ Downloading template
✔ Copying template
✔ Processing template
✔ Installing Ruby Gems
✖ Installing CocoaPods dependencies (this may take a few minutes)
error
[!] Do not use "pod install" from inside Rosetta2 (x86_64 emulation on arm64).
[!] - Emulated x86_64 is slower than native arm64
[!] - May result in mixed architectures in rubygems (eg: ffi_c.bundle files may be x86_64 with an arm64 interpreter)
[!] Run "env /usr/bin/arch -arm64 /bin/bash --login" then try again.
✖ Installing CocoaPods dependencies (this may take a few minutes)
error Looks like your iOS environment is not properly set. Please go to https://reactnative.dev/docs/environment-setup?os=macos&platform=android and follow the React Native CLI QuickStart guide for macOS and iOS.
info Run CLI with --verbose flag for more details.
如此反复,我一直认为是M1芯片的事情,去了RN的github也看了,各种方法各种不管用。然后我仔细看了一下
Do not use "pod install" from inside Rosetta2 (x86_64 emulation on arm64).
不让我pod install,我偏执行,然后得到
No `Podfile' found in the project directory.
我打开文件目录看到在AwesomeProject的一级目录确实没有啊,联想之前看了众多资料,看到一个ios文件夹,打开一看,在里面有个Podfile,这不才是该pod install的目录吗?????
于是在终端已经在AwesomeProject文件目录下的条件下,
cd ios
pod install
然后就…居然开始装第三方包了… 我不懂为什么官网不提这个,很坑人啊…
也许在pod install过程中会卡在某个包很久,这时候果断结束任务,重新pod install,不要怕重新下载之前下载好的,pod会自动检测是否安装过,如果会跳过,但在终端界面,会给你一种重新安装的错觉。
就这样pod install直到看到下面这个
Pod install took 83 [s] to run Integrating client project
说明安装完了,在ios的目录下,执行
yarn ios
等待 building app… ,也许会花费一点时间,耐心等待,最后你的虚拟手机会开机,安装app,打开界面,大功告成!好不容易。