skip to content

In a React Native app on the Android emulator, why does fetch('http://localhost:3000/api') fail, and which address reaches your development machine?

level: juniorimportance: must knowfreq 62%

answer

  1. whose loopback is localhost?
  2. the emulator is its own device
  3. a fixed 10.0.x.x alias for the host
  4. iOS Simulator shares the Mac's network
  5. real phone: LAN IP, server on 0.0.0.0

basics

~20 s

On the Android emulator, localhost is the emulator itself, not your computer. The stock emulator reaches the host's loopback through 10.0.2.2; the iOS Simulator shares the Mac's network, so localhost works there, and a physical phone needs the computer's LAN IP.

solid answer

~40 s

The Android emulator is a separate virtual device with its own network stack, so `localhost` and `127.0.0.1` inside it point at the emulator, where nothing listens on port 3000. The stock emulator exposes the host's loopback at the fixed alias `10.0.2.2` (Genymotion uses `10.0.3.2`). The iOS Simulator runs as a process on the Mac and shares its network, so `localhost` works there. A physical phone needs the computer's LAN IP, and the dev server must listen on `0.0.0.0` rather than only `127.0.0.1`. The confusing part is that the Metro bundle loads fine: React Native's Android dev-support code already swaps in `10.0.2.2` for the Metro host, but it never rewrites your own `fetch` URLs. I pick the base URL per platform with `Platform.select` behind an environment switch.

code

typescript · 11 lines
typescript
import { Platform } from 'react-native';

const DEV_HOST =
  process.env.EXPO_PUBLIC_DEV_API_HOST ??
  Platform.select({
    android: 'http://10.0.2.2:3000',
    ios: 'http://localhost:3000',
    default: 'http://localhost:3000',
  });

export const API_URL = __DEV__ ? DEV_HOST : 'https://api.example.com';

go deeper

for a junior

Recall that localhost on the Android emulator is the emulator itself, that 10.0.2.2 reaches the host, and that the iOS Simulator can use localhost directly.

for a middle

Explain why Metro connects but your fetch fails: dev-support code picks the emulator alias for the dev server only. Cover physical devices, LAN IPs and servers bound to 0.0.0.0.

for a senior

Show a base-URL module driven by configuration with per-platform fallbacks, and walk through diagnosing a device that cannot reach a laptop: bind address, firewall, VPN, client isolation.

for a principal

Frame local API access as developer-experience infrastructure: shared dev hosts or tunnels versus per-laptop servers, and keeping one configuration path so no hard-coded host ever ships.

## What localhost means inside a device `localhost` always means **the machine the code is running on**. In a React Native app, your JavaScript runs on the device (or the emulator), and `fetch` hands the URL to the device's native HTTP stack. So the question is never "where is my API?" but "which network stack resolves this hostname?" - **Android emulator** – a full virtual device with its own virtual network. Its `localhost` is the emulator's own loopback interface. Your API server runs on the host computer, not in the emulator, so a request to `http://localhost:3000` finds nothing listening and fails with a network error. - **iOS Simulator** – not a virtual machine. It runs as a macOS process and shares the Mac's network interfaces, so `localhost` in the simulator *is* the Mac, and the same URL works. - **Physical device** – a separate computer on your Wi-Fi. Neither `localhost` nor the emulator alias exists for it; it needs your computer's address on the local network. ## The host aliases The Android emulator routes one special address to the host's loopback. React Native's own Android sources name these constants (`EMULATOR_LOCALHOST`, `GENYMOTION_LOCALHOST`): | Where the app runs | Address that reaches your computer | Notes | |---|---|---| | Stock Android emulator | `10.0.2.2` | A fixed alias for the host's `127.0.0.1` | | Genymotion emulator | `10.0.3.2` | Same idea, different virtual network | | iOS Simulator | `localhost` / `127.0.0.1` | Shares the Mac's network | | Physical phone on Wi-Fi | The computer's LAN IP, e.g. `192.168.1.20` | Server must listen on all interfaces | Because `10.0.2.2` maps onto the host's loopback, a server bound only to `127.0.0.1` is still reachable from the emulator. A physical phone, however, arrives on the LAN interface, so the server must bind to `0.0.0.0` (all interfaces), the firewall must allow the port, and phone and computer must share a network. ## Why the bundle loads but your API call does not This is the part that confuses people in the interview and in real life. In a debug build, the app downloads its JavaScript from **Metro** on your computer, and that works on the emulator without any configuration. The reason is that React Native's Android dev-support code detects a stock emulator and uses `10.0.2.2` for the Metro host automatically (and `10.0.3.2` on Genymotion). That rewrite applies **only to the dev server connection**. Your `fetch('http://localhost:3000/api')` is passed to the native networking layer exactly as written. So "Metro works, my API doesn't" is the expected symptom, not a sign that networking is broken. ## Picking the base URL Keep the host decision in one module instead of scattering URLs through screens: 1. Read the development host from configuration (an environment variable or app config), so a teammate on a physical phone can point it at their own LAN IP. 2. Fall back per platform with `Platform.select`, which accepts `android`, `ios`, `native` and `default` keys. 3. Use the production HTTPS URL outside `__DEV__`. A few more things that commonly get in the way: - A plain `http://` URL also has to satisfy each platform's cleartext-traffic rules, which is a separate transport-security topic. - Port forwarding over USB is another way to reach the host from an Android device; it belongs with device setup rather than with `fetch`. - A VPN on the computer or a guest Wi-Fi with client isolation can block phone-to-laptop traffic even when the IP is right. ## Debugging it step by step When a request from the app fails and you suspect the host, narrow it down in order: 1. **Open the URL from the device's own browser.** If Chrome inside the emulator cannot load `http://10.0.2.2:3000/health`, the problem is the network path, not your JavaScript. 2. **Log the resolved base URL at startup** in development, so a stale environment value or a wrong `Platform.select` branch is visible immediately. 3. **Check the server's bind address** with the server's own startup log: `127.0.0.1` is fine for emulators, but a physical phone needs `0.0.0.0`. 4. **Read the error, not just the spinner.** A connection refused arrives quickly; a request that hangs points at a firewall or an unreachable address, which is also why every request should have a timeout. ## How to talk about it in an interview A strong answer states the mechanism in one line (**the emulator has its own loopback**), gives the alias (**`10.0.2.2`**), contrasts the iOS Simulator (**shared network**) and the physical device (**LAN IP plus a server listening on all interfaces**), and then shows that you centralise the base URL. Mentioning why Metro still works shows you understand that React Native's tooling and your application code resolve hosts separately.

  • The same URL with 10.0.2.2 works on the emulator but times out on a physical Android phone. What do you check?
    `10.0.2.2` exists only inside the emulator's virtual network, so the phone needs the computer's LAN IP. Then check that the server listens on `0.0.0.0`, not just `127.0.0.1`, that the host firewall allows the port, and that phone and computer share a network without client isolation or a VPN in between.
  • Why doesn't the iOS Simulator need an alias at all?
    The iOS Simulator is not a virtual machine with its own network; it runs as a macOS process and uses the Mac's network interfaces. `localhost` inside it is therefore the Mac itself, so a server on the Mac's port 3000 answers directly.
  • Where should the development host live so the team does not edit code to switch devices?
    In configuration: an environment variable or app config value read by one API module, with `Platform.select` defaults as a fallback. Each developer overrides it locally, for example with their LAN IP, and release builds always use the production HTTPS URL.

The emulator is a flat inside your house with its own front door: shouting 'hello, this flat' reaches only the flat. 10.0.2.2 is the intercom button labelled 'main house', the one fixed way to ring the building you are standing in.

  • The emulator shares the computer's network, so localhost reaches the host.
  • React Native rewrites localhost in fetch URLs automatically in debug builds.
  • 10.0.2.2 also works from a physical Android phone on the same Wi-Fi.
  • If Metro loads the bundle, the API must be reachable at localhost too.
  • The iOS Simulator needs 10.0.2.2 just like the Android emulator.