Open the terminal and configure the ANDROID_HOME environment variable, to do that run the following command :
open -e .zprofile
Above command will open the zprofile file in the text editor
Copy the below lines into the zprofile file :
export ANDROID_HOME=$HOME/Library/Android/sdk export PATH=$PATH:$ANDROID_HOME/emulator export PATH=$PATH:$ANDROID_HOME/platform-tools
Then use the following command to update the zprofile file :
source ~/.zprofile
Create a folder where you want to create your React Native project, then open your VScode and drag and drop this folder into the VScode window, this will open that folder into VScode, now use the shortcut " Ctrl + ` " to open the inbuilt terminal in VScode.
In the terminal use the following command :
npx react-native init FirstProject
In the above command :
npx is Node Package Executer which executes any package
react-native is the Utility
init is the command
FirstProject is the name of our project (Feel free to give it any name)
This will initialize a React Native project named FirstProject inside the folder that we had opened inside VScode
If the command in Step 2 gave some error regarding the Ruby version then run the following commands in the terminal :
brew update brew install ruby-build "After running above line you will receive a command at the end just copy and paste it and execute it then use below command" brew install rbenv
Note: For the above commands to work you must have homebrew installed on your mac
To set up your Android device go to Developer Options -> Enable USB Debugging and Select USB Configuration to MTP/FTP/File Transfer, now you are ready to connect your Android device to your Mac
If you want to use AVD(Android Virtual Device) you can follow the official React Native Documentation for it, Visit Preparing the Android device section
Now open VScode terminal and go into your project folder :
cd FirstProject/
Change the above command according to your project name
Then run the below command :
npx react-native run-android
If everything is fine a window will pop up which will look like the below :
Now type "a" to run on Android
If the build fails, delete the node_modules folder and then again download it using the following command :
npm install
Finally follow Step 6 again and you will have your first React Native app successfully started on your smartphone or AVD which looks like the below :
This article is based on the video "How to start your first React Native app and debugging" by Hitesh Choudhary from his React Native series on YouTube