react native安裝環(huán)境及使用vscode調(diào)試debug

React Native搭建環(huán)境:https://www.react-native.cn/docs/environment-setup
React Native基礎(chǔ)入門:https://www.react-native.cn/docs/getting-started
深入 JSX:https://zh-hans.reactjs.org/docs/jsx-in-depth.html#gatsby-focus-wrapper
React:https://zh-hans.reactjs.org/
https://reactnative.dev/docs/debugging


一绝编、安裝環(huán)境

brew install node
brew install watchman
npm install -g yarn
xcode-select --switch /Applications/Xcode.app/Contents/Developer/
參考:
【Homebrew】Mac安裝node報錯
React Native 中文網(wǎng) 安裝環(huán)境

二浪南、創(chuàng)建新項目

2.1.1 全新的項目
npx react-native init AwesomeProject
如果本地安裝的pod與rn默認的不一致,會報錯

Installing dependencies
? CocoaPods (https://cocoapods.org/) is not installed. CocoaPods is necessary for the iOS project to run correctly. Do you want to install it? ? Yes, with gem (may require sudo)
? Installing CocoaPods
? Installing CocoaPods dependencies (this may take a few minutes)
? Installing CocoaPods dependencies (this may take a few minutes)
error Error: Failed to install CocoaPods dependencies for iOS project, which is required by this template.
Please try again manually: "cd ./Sample3/ios && pod install".
CocoaPods documentation: https://cocoapods.org/

使用跳過install即可
npx react-native init AwesomeProject --skip-install

2.1.2 如果已經(jīng)存在的項目拥娄,比如GitHub或內(nèi)網(wǎng)git上的上面签赃,一般都沒有沒有node_modules文件夾上真,需要在主路徑(在package.json所在的文件夾路徑)所在的文件夾路徑里使用命令生成
npm install --force

2.3 進入ios目錄,運行pod
cd ios
bundle install
pod install

2.4 在主路徑(在package.json所在的文件夾路徑)里使用默認的8081端口運行
yarn ios
等同于:yarn ios --port=8081
等同于:react-native run-ios --port=8081
等同于:react-native run-ios
查看被占用的端口:lsof -i :8081
刪除指定pid的服務(wù):kill pid數(shù)字


三仓手、多項目同時運行

如果同時運行多個rn項目,每個rn項目都會有一個node服務(wù)器玻淑,第一個項目占用了默認的8081端口后嗽冒,其他項目就運行不起來了,需要給后續(xù)的項目設(shè)置不同的端口

3.1臨時修改運行端口:
yarn ios --port=8082
等同于:react-native run-ios --port=8082
然后再運行項目
3.2 永久修改端口
項目下->package.json->scripts->"ios": "react-native run-ios --port=8083"
然后再運行:yarn ios就會運行8083的端口了

port.png

VSCode-Debug
安裝插件:React Native Tools
選中Debug:


rn_debug_0.png
rn_debug_1.png
rn_debug_2.png

配置debug
https://www.react-native.cn/docs/debugging
yarn global add react-devtools
react-devtools

配置指定版本的模擬器或真機
React-Native 使用真機和指定模擬器調(diào)試

查看模擬器和真機列表:
獲取模擬列表:xcrun simctl list devic
獲取真機和模擬列表 :xcrun xctrace list devices

xcrun xctrace list devices
== Devices ==
aaa iMac (xxx-xx-xxx-xxx-xxx)
aaa的iPhone (15.3.1) (xxxx-xxxx)

== Simulators ==

Apple TV Simulator (15.4) (xxx-xx-xxx-xxx-xxx)
Apple TV 4K (2nd generation) Simulator (15.4) (xxx-xx-xxx-xxx-xxx)
Apple TV 4K (at 1080p) (2nd generation) Simulator (15.4) (xxx-xx-xxx-xxx-xxx)
iPad (9th generation) Simulator (15.4) (xxx-xx-xxx-xxx-xxx)
iPad Air (5th generation) Simulator (15.4) (xxx-xx-xxx-xxx-xxx)
iPad Pro (11-inch) (3rd generation) Simulator (15.4) (xxx-xx-xxx-xxx-xxx)
iPad Pro (12.9-inch) (5th generation) Simulator (15.4) (xxx-xx-xxx-xxx-xxx)
iPad Pro (9.7-inch) Simulator (15.4) (xxx-xx-xxx-xxx-xxx)
iPad mini (6th generation) Simulator (15.4) (xxx-xx-xxx-xxx-xxx)
iPhone 11 Simulator (15.4) (xxx-xx-xxx-xxx-xxx)
iPhone 11 Pro Simulator (15.4) (xxx-xx-xxx-xxx-xxx)
iPhone 11 Pro Max Simulator (15.4) (xxx-xx-xxx-xxx-xxx)
iPhone 12 Simulator (15.4) (xxx-xx-xxx-xxx-xxx)
iPhone 12 Pro Simulator (15.4) (xxx-xx-xxx-xxx-xxx)
iPhone 12 Pro Simulator (15.4) + Apple Watch Series 5 - 40mm (8.5) (xxx-xx-xxx-xxx-xxx)
iPhone 12 Pro Max Simulator (15.4) (xxx-xx-xxx-xxx-xxx)
iPhone 12 Pro Max Simulator (15.4) + Apple Watch Series 5 - 44mm (8.5) (xxx-xx-xxx-xxx-xxx)
iPhone 12 mini Simulator (15.4) (xxx-xx-xxx-xxx-xxx)
iPhone 13 Simulator (15.4) (xxx-xx-xxx-xxx-xxx)
iPhone 13 Simulator (15.4) + Apple Watch Series 7 - 45mm (8.5) (xxx-xx-xxx-xxx-xxx)
iPhone 13 Pro Simulator (15.4) (xxx-xx-xxx-xxx-xxx)
iPhone 13 Pro Simulator (15.4) + Apple Watch Series 6 - 40mm (8.5) (xxx-xx-xxx-xxx-xxx)
iPhone 13 Pro Max Simulator (15.4) (xxx-xx-xxx-xxx-xxx)
iPhone 13 Pro Max Simulator (15.4) + Apple Watch Series 6 - 44mm (8.5) (xxx-xx-xxx-xxx-xxx)
iPhone 13 mini Simulator (15.4) (xxx-xx-xxx-xxx-xxx)
iPhone 13 mini Simulator (15.4) + Apple Watch Series 7 - 41mm (8.5) (xxx-xx-xxx-xxx-xxx)
iPhone 8 Simulator (15.4) (xxx-xx-xxx-xxx-xxx)
iPhone 8 Plus Simulator (15.4) (xxx-xx-xxx-xxx-xxx)
iPhone SE (3rd generation) Simulator (15.4) (xxx-xx-xxx-xxx-xxx)
iPod touch (7th generation) Simulator (15.4) (xxx-xx-xxx-xxx-xxx)

使用runArguments配置指定的模擬器或真機
配置的調(diào)試開始文件位于項目路徑下: xxx/.vscode/launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug iOS",
            "cwd": "${workspaceFolder}",
            "type": "reactnative",
            "request": "launch",
            "platform": "ios",
            // 不寫runArguments參數(shù)就是運行默認的
        },
        {
            "name": "Debug iPhone 12 mini",
            "cwd": "${workspaceFolder}",
            "type": "reactnative",
            "request": "launch",
            "platform": "ios",
            // 不寫runArguments參數(shù)就是運行默認的
            "runArguments": [
                "--simulator", // "真機就是`--device`"
                "iPhone 12 mini", // 對應(yīng)模擬器的名,不寫使用默認的模擬器补履,只有需要指定某個固定模擬器才有這個配置
            ]
        },
        {
            "name": "Run 真機",
            "cwd": "${workspaceFolder}",
            "type": "reactnative",
            "request": "launch",
            "platform": "ios",
            // 不寫runArguments參數(shù)就是運行默認的
            "runArguments": [
                "--device", // "真機就是`--device`"
                //"aaaa的iPhone", // 對應(yīng)真機的名字(一般只有連接了多個真機才需要寫名字)酒甸,不寫使用默認的
            ]
        }
    ]
}

二宽涌、遇到問題

==> Installing dependencies for node: icu4c, libnghttp2 and libuv
==> Installing node dependency: icu4c
==> Pouring icu4c-69.1.monterey.bottle.tar.gz
Error: No such file or directory @ rb_sysopen - /Users/xxx/Library/Caches/Homebrew/downloads/048f0b707abd0b1d6047b4a64f7a58f52d6c9d41447508c5fbf85c71e2cb052f--icu4c-69.1.monterey.bottle.tar.gz

這里是icu4c沒安裝成功,命令安裝下
brew install icu4c

==> Installing dependencies for node: libnghttp2
==> Installing node dependency: libnghttp2
==> Pouring libnghttp2-1.46.0.monterey.bottle.tar.gz
Error: No such file or directory @ rb_sysopen - /Users/aaa/Library/Caches/Homebrew/downloads/418b0967ba69602e95d0994dced17956cdbbbb12c7d3c146d2ca7734ea06bd42--libnghttp2-1.46.0.monterey.bottle.tar.gz

手動安裝 brew install libnghttp2

然后再安裝brew install node

2炉菲、

Error: python@3.10: the bottle needs the Apple Command Line Tools to be installed.
You can install them, if desired, with:
xcode-select --install

3

? Installing CocoaPods dependencies (this may take a few minutes)
? Installing CocoaPods dependencies (this may take a few minutes)
error Error: Failed to install CocoaPods dependencies for iOS project, which is required by this template.

4

Warning: the running version of Bundler (2.1.4) is older than the version that created the lockfile (2.2.27). We suggest you to upgrade to the version that created the lockfile by running gem install bundler:2.2.27.
Traceback (most recent call last):
4: from /Users/aaa/.rvm/rubies/ruby-2.7.4/bin/ruby_executable_hooks:22:in <main>' 3: from /Users/aaa/.rvm/rubies/ruby-2.7.4/bin/ruby_executable_hooks:22:ineval'
2: from /usr/local/bin/pod:23:in <main>' 1: from /Users/aaa/.rvm/rubies/ruby-2.7.4/lib/ruby/2.7.0/rubygems.rb:296:inactivate_bin_path'
/Users/aaa/.rvm/rubies/ruby-2.7.4/lib/ruby/2.7.0/rubygems.rb:277:in `find_spec_for_exe': can't find gem cocoapods (>= 0.a) with executable pod (Gem::GemNotFoundException)

5

can't find gem cocoapods (>= 0.a) with executable pod (Gem::GemNotFoundException)

pod install 報錯: can't find gem cocoapods (>= 0.a) with executable pod (Gem::GemNotFoundException)

6

Could not find proper version of cocoapods (1.11.3) in any of the sources
Run bundle install to install missing gems.

bundle install

/bin/sh: react-native: command not found

npm install
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: qieziProject@0.0.1
npm ERR! Found: react@17.0.2
npm ERR! node_modules/react
npm ERR! react@"17.0.2" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer react@"^16.8" from @react-native-community/async-storage@1.12.1
npm ERR! node_modules/@react-native-community/async-storage
npm ERR! @react-native-community/async-storage@"^1.12.1" from the root project
npm ERR!
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps

存在的項目,沒有node_modules文件夾的:npm install --force
React-Native(node_modules)

Mac 查看端口和進程
查看對應(yīng)的端口號
lsof -i :8081
刪除對應(yīng)的端口號占用
kill pid的號

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末舶替,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌氛堕,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,042評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件野蝇,死亡現(xiàn)場離奇詭異岔擂,居然都是意外死亡,警方通過查閱死者的電腦和手機浪耘,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 89,996評論 2 384
  • 文/潘曉璐 我一進店門乱灵,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人七冲,你說我怎么就攤上這事痛倚。” “怎么了澜躺?”我有些...
    開封第一講書人閱讀 156,674評論 0 345
  • 文/不壞的土叔 我叫張陵蝉稳,是天一觀的道長。 經(jīng)常有香客問我掘鄙,道長耘戚,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,340評論 1 283
  • 正文 為了忘掉前任操漠,我火速辦了婚禮收津,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘浊伙。我一直安慰自己撞秋,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 65,404評論 5 384
  • 文/花漫 我一把揭開白布嚣鄙。 她就那樣靜靜地躺著吻贿,像睡著了一般。 火紅的嫁衣襯著肌膚如雪哑子。 梳的紋絲不亂的頭發(fā)上舅列,一...
    開封第一講書人閱讀 49,749評論 1 289
  • 那天,我揣著相機與錄音卧蜓,去河邊找鬼帐要。 笑死,一個胖子當(dāng)著我的面吹牛烦却,可吹牛的內(nèi)容都是我干的宠叼。 我是一名探鬼主播,決...
    沈念sama閱讀 38,902評論 3 405
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼冒冬!你這毒婦竟也來了伸蚯?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,662評論 0 266
  • 序言:老撾萬榮一對情侶失蹤简烤,失蹤者是張志新(化名)和其女友劉穎剂邮,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體横侦,經(jīng)...
    沈念sama閱讀 44,110評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡挥萌,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,451評論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了枉侧。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片引瀑。...
    茶點故事閱讀 38,577評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖榨馁,靈堂內(nèi)的尸體忽然破棺而出憨栽,到底是詐尸還是另有隱情,我是刑警寧澤翼虫,帶...
    沈念sama閱讀 34,258評論 4 328
  • 正文 年R本政府宣布屑柔,位于F島的核電站,受9級特大地震影響珍剑,放射性物質(zhì)發(fā)生泄漏掸宛。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 39,848評論 3 312
  • 文/蒙蒙 一招拙、第九天 我趴在偏房一處隱蔽的房頂上張望唧瘾。 院中可真熱鬧,春花似錦迫像、人聲如沸劈愚。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,726評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至掠械,卻和暖如春由缆,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背猾蒂。 一陣腳步聲響...
    開封第一講書人閱讀 31,952評論 1 264
  • 我被黑心中介騙來泰國打工均唉, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人肚菠。 一個月前我還...
    沈念sama閱讀 46,271評論 2 360
  • 正文 我出身青樓舔箭,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子层扶,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 43,452評論 2 348

推薦閱讀更多精彩內(nèi)容