You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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.json file and check the scripts section. If there’s no start-android-emulator entry, 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 latest level):
    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 ~/.zshrc if 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:
    emulator -avd MyReactNativeEmulator
    
    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.

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 android fails, check if adb detects your emulator with adb devices. If not, restart adb with adb kill-server && adb start-server and try again.

内容的提问来源于stack exchange,提问作者Nido

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 20:38:10