Metro Bundler for React Native

0.84.3 · active · verified Sun Apr 19

Metro is the JavaScript bundler specifically designed for React Native applications, providing optimized bundling, hot module reloading (HMR), and Fast Refresh capabilities crucial for efficient mobile development workflows. It is currently at version 0.84.3 and typically sees frequent minor releases, incorporating new features, bug fixes, and performance improvements. Key differentiators include its deep integration with the React Native ecosystem, advanced caching mechanisms for fast rebuilds, and robust support for various configuration options, including CommonJS, ESM, and TypeScript configuration files. Metro focuses on delivering a high-performance and streamlined developer experience, enabling rapid iteration cycles for large-scale React Native projects. It requires Node.js versions ^20.19.4 || ^22.13.0 || ^24.3.0 || >= 25.0.0.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates a typical `metro.config.js` file for a React Native project, showing how to customize transformer, resolver, and server settings while extending Metro's default configuration. This file is typically run via the React Native CLI (e.g., `npx react-native start`).

import path from 'path';
import { getDefaultConfig } from 'metro-config';

/**
 * Metro configuration for a React Native project.
 * See: https://facebook.github.io/metro/docs/configuration
 *
 * @type {import('metro-config').MetroConfig}
 */
const config = (async () => {
  const { resolver: { sourceExts, assetExts } } = await getDefaultConfig(__dirname);

  return {
    transformer: {
      babelTransformerPath: require.resolve('metro-react-native-babel-transformer'),
      enableBabelRCLookup: true,
      // Optionally customize babel transformer options
      // getTransformOptions: async () => ({
      //   transform: {
      //     experimentalImportSupport: false,
      //     inlineRequires: true,
      //   },
      // }),
    },
    resolver: {
      assetExts: [...assetExts, 'gltf', 'glb', 'bin', 'webp', 'mp3', 'mp4'],
      sourceExts: [...sourceExts, 'mjs', 'cjs', 'ts', 'tsx'],
    },
    server: {
      port: process.env.METRO_PORT ? parseInt(process.env.METRO_PORT, 10) : 8081,
      // Enable HTTPS server with your own key and certificate:
      // https: {
      //   key: path.resolve(__dirname, './ssl/key.pem'),
      //   cert: path.resolve(__dirname, './ssl/cert.pem'),
      // },
    },
    // Add additional directories to watch for changes, useful in monorepos.
    watchFolders: [
      // path.resolve(__dirname, '../../packages/my-shared-components'),
    ],
    // Cache configuration (defaults to FileStore).
    cacheStores: [],
  };
})();

module.exports = config;

view raw JSON →