Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Environment Setup  ›  Lesson

Metro, the Bundler

Metro Bundler 10 minMetro resolves every import, transforms JSX and TypeScript into plain JavaScript, and serves one bundle to the app during development.

Metro, the Bundler

When you run npx expo start for Sprout, a terminal fills with a QR code and a line that says Metro is waiting. Your phone never opens your source files directly. Something has to collect them and hand them over. That something is Metro.

The rule

Metro gathers every file into one bundle and serves it to the app.

Metro is the JavaScript bundler made for React Native. It does three jobs:

  1. Resolve. Starting at your entry file, it follows every import and require, including packages in node_modules and images in assets/.
  2. Transform. It compiles JSX and TypeScript into plain JavaScript the engine can run, using Babel.
  3. Serve. In development it runs a local server (port 8081 by default) and sends the bundle to the app on your phone or simulator.
npx expo start          # starts Metro for an Expo project
npx expo start --clear  # starts Metro with an empty cache

In a bare CLI project the command is npx react-native start, and the cache flag is --reset-cache.

Because Metro watches your files, a save triggers a quick rebuild of just what changed, and Fast Refresh pushes it to the running app. For a release build, Metro produces one bundle that is packed inside the app, so the store version needs no server.

Metro also understands platform extensions: if both Header.ios.js and Header.android.js exist, import Header from './Header' picks the right one for each platform. You will use that in the platform section.

The shortcut

When the app shows old code or a "module not found" error, walk Metro's three jobs in order: resolve, transform, serve. The problem is almost always at one of those steps.

Sprout says "Unable to resolve module ./PlantCard"Step 1 of 4
  1. Resolve?

    Metro could not find the file. Check the path: the file is components/PlantCard.js.

The trap is thinking the phone app "contains" your code during development. It does not; it downloads the bundle from Metro. If your phone and laptop are on different networks, the app cannot reach Metro and loading fails.

Drills

Answer fast — trust the rule.

quick drill

What is Metro's main job?

quick drill

Which port does Metro's development server use by default?

quick drill

Sprout keeps showing old behaviour after you install a new package. What is a sensible first fix in an Expo project?

quick drill

During development, where does the app on your phone get its JavaScript from?

Remember

  • Metro gathers every file into one bundle and serves it to the app.
  • Debug in order: resolve, transform, serve.
  • Trap: in development the phone needs to reach Metro on your computer.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainDebugging with Metro's three jobs

Your app shows 'Unable to resolve module ./utils/dates'. Explain what Metro does, which of its jobs failed here, and how you would fix it.