Azure Linux在线VSCode运行React Native安卓模拟器npm报错求助
Fixing React Native Android Emulator Run Issue on Azure Linux VM (No Android Studio Needed)
Hey there! Let's get your React Native app running on an Android emulator without installing Android Studio—let's break this down step by step.
First, let's tackle that npm error you're seeing. The message about /home/vsonline/.npmrc pops up because npm doesn’t recognize the start-android-emulator command—it’s not a built-in npm command, and it looks like you don’t have it defined as a script in your package.json either. Let’s fix that first, then set up the emulator properly.
Step 1: Fix the npm Command Confusion
- Open your project's
package.jsonfile and check thescriptssection. If there’s nostart-android-emulatorentry, add it (or use React Native’s default commands):"scripts": { "start": "react-native start", "android": "react-native run-android", "start-emulator": "emulator -avd YourEmulatorName" // Replace with your emulator's name later } - If you just want to run your app on an emulator, the standard workflow is: start the emulator first, then run
npm run android(this handles starting Metro and deploying the app automatically).
Step 2: Set Up Android Emulator Without Android Studio
We’ll use the Android Command Line Tools to set everything up directly from the terminal:
1. Install Android Command Line Tools
- Create the required directory structure first:
mkdir -p ~/Android/Sdk/cmdline-tools/latest - Download the Linux commandlinetools package via terminal:
wget https://dl.google.com/android/repository/commandlinetools-linux-10406996_latest.zip - Unzip the file into the directory you created:
unzip commandlinetools-linux-10406996_latest.zip -d ~/Android/Sdk/cmdline-tools/latest - Fix the directory structure (the tools expect files at the
latestlevel):mv ~/Android/Sdk/cmdline-tools/latest/cmdline-tools/* ~/Android/Sdk/cmdline-tools/latest/ rm -rf ~/Android/Sdk/cmdline-tools/latest/cmdline-tools
2. Configure Environment Variables
- Open your shell config file (use
~/.zshrcif you’re using Zsh instead of Bash):nano ~/.bashrc - Add these lines at the end to set up Android paths:
export ANDROID_HOME=~/Android/Sdk export PATH=$PATH:$ANDROID_HOME/emulator export PATH=$PATH:$ANDROID_HOME/platform-tools export PATH=$PATH:$ANDROID_HOME/cmdline-tools/latest/bin - Save and exit, then reload the config to apply changes:
source ~/.bashrc
3. Install Required Android Components
- Install a supported Android platform version (we’ll use Android 33, which works well with most React Native versions):
sdkmanager "platforms;android-33" - Install a system image for the emulator:
sdkmanager "system-images;android-33;google_apis;x86_64" - Install the emulator and platform tools (adb, etc.):
sdkmanager "emulator" "platform-tools"
4. Create and Start the Emulator
- Create a new Android Virtual Device (AVD) with a Pixel 5 profile (you can change the name or device type if you want):
avdmanager create avd -n MyReactNativeEmulator -k "system-images;android-33;google_apis;x86_64" --device "pixel_5" - Confirm your emulator was created successfully:
emulator -list-avds - Start the emulator:
Critical Note: Azure VMs need nested virtualization enabled for the emulator to run smoothly. You can turn this on in the Azure Portal: go to your VM > Settings > Size > Change size, select a VM size that supports nested virtualization (like Dv3/Ev3 series), and enable the option.emulator -avd MyReactNativeEmulator
Step 3: Run Your React Native App
- Open one terminal window and start the Metro bundler:
npm start - Open a second terminal window and deploy your app to the running emulator:
npm run android
Quick Troubleshooting Tips
- If the emulator is laggy, ensure your Azure VM has at least 2 CPU cores and 4GB of RAM, and nested virtualization is enabled.
- If
npm run androidfails, check ifadbdetects your emulator withadb devices. If not, restart adb withadb kill-server && adb start-serverand try again.
内容的提问来源于stack exchange,提问作者Nido
相关产品推荐
相关产品推荐

