Chapter 2: Setting Up the Development Environment
Setting up the right development environment is crucial for React Native development. In this chapter, we'll cover the steps required to install the necessary tools, create your first Expo project, and run it on a simulator or physical device.
1. Installing Node.js and npm
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. It's essential for running JavaScript code outside the browser, which is necessary for React Native development. npm (Node Package Manager) is bundled with Node.js and is used to install and manage JavaScript packages.
Step-by-Step Guide:
-
Download and Install Node.js:
- Go to the Node.js official website.
- Download the LTS (Long-Term Support) version suitable for your operating system (Windows, macOS, or Linux).
- Follow the installation instructions provided on the website.
Example: On macOS, after downloading the installer, double-click the
.pkgfile and follow the on-screen instructions. -
Verify Installation:
-
Open your terminal (Command Prompt, PowerShell, Terminal, etc.).
-
Run the following commands to verify the installation:
node -v npm -v -
You should see the installed versions of Node.js and npm displayed.
Example Output:
v18.12.0 9.6.0 -
-
Upgrade npm (Optional):
-
If you want to ensure you have the latest version of npm, run:
npm install -g npm@latest
-
2. Setting Up Expo CLI
Expo CLI is a command-line tool that helps you build, develop, and manage React Native apps using Expo. It simplifies the process of setting up and running React Native projects.
Step-by-Step Guide:
-
Install Expo CLI:
-
Run the following command in your terminal:
npm install -g expo-cli
Example: This command will install Expo CLI globally, allowing you to use the
expocommand from anywhere in your terminal. -
-
Verify Installation:
-
Check that Expo CLI is installed correctly by running:
expo --version
Example Output:
7.0.3 -
3. Creating Your First Expo Project
Now that you have Expo CLI installed, it's time to create your first React Native project.
Step-by-Step Guide:
-
Create a New Project:
-
Navigate to the directory where you want to create your project.
-
Run the following command:
expo init MyFirstApp -
You'll be prompted to choose a template. Select the blank template for a minimal setup.
Example: After selecting the template, Expo will install the necessary dependencies and set up your project structure.
-
-
Navigate into Your Project Directory:
cd MyFirstApp -
Explore the Project Structure:
- Open the project in your code editor (e.g., VSCode).
- You should see a basic folder structure with files like
App.js,package.json, and more.
Example: The
App.jsfile is the entry point of your React Native application. It contains a simpleTextcomponent displaying "Open up App.js to start working on your app!"
4. Running the Project on a Simulator or Device
After creating your project, the next step is to run it on a simulator or a physical device.
Step-by-Step Guide:
-
Running on a Physical Device:
-
Download the Expo Go app from the App Store (iOS) or Google Play Store (Android).
-
In your terminal, make sure you're in the root directory of your project (
MyFirstApp). -
Start the development server by running:
expo start -
A QR code will appear in your terminal.
-
Open the Expo Go app on your device and scan the QR code to run the app on your device.
Example: Your React Native app should open on your device, showing the default "Welcome to React Native!" screen.
-
-
Running on a Simulator (Optional):
- If you prefer to run the app on a simulator:
- iOS:
- On macOS, you can use Xcode's iOS Simulator.
- Run
expo startand pressito open the app in the iOS Simulator.
- Android:
- Install Android Studio and set up an Android Virtual Device (AVD).
- Run
expo startand pressato open the app in the Android Emulator.
- iOS:
Example: The app should launch in the simulator, displaying the same default screen as on a physical device.
- If you prefer to run the app on a simulator:
By the end of this chapter, you should have a fully functional React Native development environment, a new Expo project, and the ability to run the app on both simulators and physical devices. This foundational setup will allow you to move forward with building and testing your React Native applications.