如何正确使用RCT_METRO_PORT更改城域束端口



我想在两个不同版本的iOS上运行测试,因此我需要metro bundler在两个不相同的端口上运行。但这是关于相同的回购,所以我不能改变8081的所有出现。此外,我不能在iOS模拟器中使用devtools手动更改端口,因为它是用于使用Detox、automatics进行e2e测试的。

我到处看到RCT_METRO_PORT的使用可以奏效,但直到现在我都没有成功。。。

因此,这里有一个简单的问题:我们如何使用RCT_METRO_PORT,以便METRO bundler在8081之外的另一个端口上运行,使用.env文件或package.json脚本中的env变量?

[编辑]:我的问题不仅是react native run ios,还包括构建版本,比如这个脚本set -o pipefail && xcodebuild -workspace ios/myapp.xcworkspace -configuration Debug -scheme myapp -destination name="iPhone 12 Pro" -derivedDataPath ios/build > /dev/null

根据这个答案,您必须从环境变量更改默认端口,有三种方式的

  1. 第一个:(本地环境(:安装react-native-dotenv,并在babel配置文件中进行配置。然后在项目的根文件夹中添加一个.env文件,并编写RCT_METRO_PORT=8590。(8590是替代端口号的样本(

  2. 第二个:(global-env(:转到bash/zsh-rc文件并export编号为8590的环境变量,例如:

    export RCT_METRO_PORT=8590
    

    提示:如果你的操作系统是windows,第二个选项遵循这个答案

  3. 第三个:(内联环境(:对于每个操作,都应该使用--port 8590:

    • 地铁:yarn start --port 8590
    • 安卓系统:yarn android --port 8590
    • iOS:yarn ios --port 8590

TEST :在终端中运行echo $RCT_METRO_PORT以测试第一种和第二种方式,以确保设置了env-var。

iOS提示 :对于项目的ios文件夹,请查找Pods文件夹,在Pods文件夹中查找RCTDefines.h文件,共有两个文件,其中都将8081更改为8590

要连接到React Native Debugger,请按É+t并将8081端口更改为8590

这就是如何在package.json中管理dev和stage env的不同端口

DOCS

"scripts": {
"android:dev": "RCT_METRO_PORT=8081 react-native run-android",
"android:stage": "RCT_METRO_PORT=9091 react-native run-android",
"ios:dev": "RCT_METRO_PORT=8081 react-native run-ios",
"ios:stage": "RCT_METRO_PORT=9091 react-native run-ios",
"start:dev": "react-native start --port 8081",
"start:stage": "react-native start --port 9091",
"build:dev": "export RCT_METRO_PORT=8081 && set -o pipefail && xcodebuild -workspace ios/myapp.xcworkspace -configuration Debug -scheme myapp -destination name="iPhone 12 Pro" -derivedDataPath ios/build > /dev/null",
"build:stage": "export RCT_METRO_PORT=9091 && set -o pipefail && xcodebuild -workspace ios/myapp.xcworkspace -configuration Debug -scheme myapp -destination name="iPhone 12 Pro" -derivedDataPath ios/build > /dev/null",
}

"scripts": {
"android:dev": "react-native run-android --port 8081",
"android:stage": "react-native run-android --port  9091",
"ios:dev": "react-native run-ios --port 8081",
"ios:stage": "react-native run-ios --port  9091",
"start:dev": "react-native start --port 8081",
"start:stage": "react-native start --port 9091",
"build:dev": "export RCT_METRO_PORT=8081 && set -o pipefail && xcodebuild -workspace ios/myapp.xcworkspace -configuration Debug -scheme myapp -destination name="iPhone 12 Pro" -derivedDataPath ios/build > /dev/null",
"build:stage": "export RCT_METRO_PORT=9091 && set -o pipefail && xcodebuild -workspace ios/myapp.xcworkspace -configuration Debug -scheme myapp -destination name="iPhone 12 Pro" -derivedDataPath ios/build > /dev/null",
}

最新更新