React Native 运行环境安装:0.6以下/以上版本的 新老项目 都适用

用新环境运行老RN项目,或者换电脑运行RN项目,环境配置不对会引起项目无法执行,建议先重新安装环境,然后执行(因为RN项目的运行 与本地环境,终端脚本执行顺序,模拟器版本等都有关系)。

1.卸载Homebrew  并重新安装

   1.1先卸载Homebrew

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"

  1.2 然后再安装

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

参考链接:https://www.jianshu.com/p/133705e0ae74?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

1.3注意:

在macOS High Sierra(10.13.6)及以上版本的Mac上面,使用brew install *** 安装时老是报错 :Permission denied权限不足问题等等,因为苹果已经不允许Homebrew作为根来安装了。

请执行:

$ sudo chown -R `whoami` /usr/local/Homebrew/
$ sudo chown -R $(whoami) $(brew --prefix)/*
$ sudo mkdir /usr/local/Frameworks
$ sudo chown -R `whoami` /usr/local/Frameworks/

参考链接:https://blog.csdn.net/meifannao789456/article/details/105083605)

2.卸载原来的环境并安装最新  node环境:

  2.1卸载node:

sudo npm uninstall npm -g
sudo rm -rf /usr/local/lib/node /usr/local/lib/node_modules /var/db/receipts/org.nodejs.*
sudo rm -rf /usr/local/include/node /Users/$USER/.npm
sudo rm /usr/local/bin/node
sudo rm /usr/local/share/man/man1/node.1
sudo rm /usr/local/lib/dtrace/node.d

 2.2验证:

//输入node 点击回车
node 
// 提示:command not found
//输入npm 点击回车
npm 
// 提示:command not found

参考链接:https://www.cnblogs.com/everlose/p/12846234.html

2.3 安装node :

brew install node

2.4   安装 watchman

brew install watchman

Watchman则是由 Facebook 提供的监视文件系统变更的工具。安装此工具可以提高开发时的性能(packager 可以快速捕捉文件的变化从而实现实时刷新)。如果你已经安装了 Node,请检查其版本是否在 v12 以上。

安装完 Node 后建议使用科学上网工具加速后面的过程,使用淘宝镜像也不可靠,但如果没有钱买代理 可以尝试使用淘宝的镜像:

# 使用nrm工具切换淘宝源
npx nrm use taobao

# 如果之后需要切换回官方源可使用
npx nrm use npm

注意:不要使用 cnpm!cnpm 安装的模块路径比较奇怪,packager 不能正常识别!

2.5 如果你发现你的终端执行脚本的时候出现 cnpm ***  *** 开头的,一定要替换为  npm  或者 yarn。

2.6 Yarn是 Facebook 提供的替代 npm 的工具,可以加速 node 模块的下载。

npm install -g yarn

安装完 yarn 之后就可以用 yarn 代替 npm 了,例如用yarn代替npm install命令,用yarn add 某第三方库名代替npm install 某第三方库名

可参考RN中文网:https://www.react-native.cn/docs/environment-setup

3. 下载 官网Node 

如果多次下载不成功,还可以从官网下载,参考链接:https://zhuanlan.zhihu.com/p/99841609 

4 .打开你的RN项目:

找到 package.json文件:

把这段代码粘贴进去:

 "scripts": {
    "0-清除安卓build文件": "rm -r -f ./android/app/build ./android/build",
    "0-清除苹果build文件": "rm -r -f ./ios/build",

    "1-清除全部build文件": "rm -r -f ./android/app/build ./android/build  ./ios/build",
    "2-清理冗余": "watchman watch-del-all",
    "3-升级": "watchman watch-del-all && rm -rf node_modules",
    "3.1-安装npm": "npm install",
    "4-关联": "react-native link",
    "5-替换文件": "cp -r MarkDown/editFile/findMatchingSimulator.js node_modules/react-native/local-cli/runIOS && cp -r MarkDown/editFile/index.js node_modules/native-echarts/src/components/Echarts && cp -r MarkDown/editFile/Ionicons.json node_modules/react-native-vector-icons/glyphmaps && cp -r MarkDown/editFile/Ionicons.ttf node_modules/react-native-vector-icons/Fonts && cp -r MarkDown/editFile/Ionicons.ttf node_modules/native-base/Fonts && cp -r MarkDown/editFile/RCTModuleMethod.mm node_modules/react-native/React/Base",
    "6-podInstall": "cd ios && pod install",
    "7-start": "node dev-server.js 8080",
    "8-运行iOS模拟器": "yarn react-native run-ios",
    "9-运行Android模拟器": "yarn react-native run-android",
    "打包安卓": "cd android/ && ./gradlew assembleRelease",
    "打开APK位置": "open ./android/app/build/outputs/apk/release",
    "npmUninstall": "npm uninstall --save react-native-device-info@0.22.5",
    "npmInstall": "npm install --save react-native-device-info@0.22.5",
    "npmInstalllink": "react-native link react-native-device-info"
  },

如图:

 执行 代码里的1-6步,然后点击

 运行即可;

 5 .

 如果修改RN内容 进行热更新而模拟器没变化 即 关联不上问题:

* 修改VSCode默认配置文件,点击左下角: 齿轮 标志图 -> 设置,搜索框输入: files.autoSave。

* 把"files.autoSave":"off"   修改成 "files.autoSave":"onFocusChange",意思是当编辑器失去焦点的时候就会自动更新文件内容到磁盘。

选择:onFocusChange 重启VScode即可。

可参考:https://blog.csdn.net/qq_38826019/article/details/114644873

6.  iOS运行模拟器 代码报错:

Cannot initialize a parameter of type 'NSArray<id<RCTBridgeModule>> *' 
                with an lvalue of type 'NSArray<Class> *__strong'
Cannot initialize a parameter of type 'NSArray<Class> *' 
                with an lvalue of type 'NSArray<id<RCTBridgeModule>> *__strong'
Cannot initialize a parameter of type 'NSArray<id<RCTBridgeModule>> *' 
                with an rvalue of type 'NSArray<Class> *'

这是由于升级XCode 12.5之后的问题,在ios/Podfile文件中加入如下的脚本即可:

post_install do |installer|
## Fix for XCode 12.5
  find_and_replace(
  "../node_modules/react-native/React/CxxBridge/RCTCxxBridge.mm",
  "_initializeModules:(NSArray<id<RCTBridgeModule>> *)modules", 
  "_initializeModules:(NSArray<Class> *)modules")
   
  find_and_replace(
  "../node_modules/react-native/ReactCommon/turbomodule/core/platform/ios/RCTTurboModuleManager.mm",
  "RCTBridgeModuleNameForClass(module))", 
  "RCTBridgeModuleNameForClass(Class(module)))"
  )
end
 
def find_and_replace(dir, findstr, replacestr)
  Dir[dir].each do |name|
      text = File.read(name)
      replace = text.gsub(findstr,replacestr)
      if text != replace
          puts "Fix: " + name
          File.open(name, "w") { |file| file.puts replace }
          STDOUT.flush
      end
  end
  Dir[dir + '*/'].each(&method(:find_and_replace))
end

然后,重新执行pod install 命令安装即可。也可以使用12.0 获取其他低版本xcode 开发工具。

参考链接:https://www.jb51.net/article/211935.htm

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

app开发工程师V帅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值