Metro is the tool React Native uses to build your JavaScript code and assets. On the web, Vite or webpack play that role, and Metro uses Babel to turn syntax such as JSX into executable JavaScript. In development it runs as a server that your app talks to. Its configuration file starts from defaults a React Native app can't build without, so any customization has to add to those defaults and keep them in the final config.
Key terms
- Metro dev server: the development server you start with
npm start, listening on port 8081 unless you change it. @react-native/metro-config: the package that holds React Native's Metro defaults and exportsgetDefaultConfigandmergeConfig.mergeConfig: a function that merges partial Metro configs, with each argument overriding the ones to its left.- Config function: a
metro.config.jsthat exports a function instead of an object, called with a base config and returning the final one.
Starting the server
Run npm start (or yarn start) from the project root. In the app template, the start script runs react-native start, and the android and ios scripts run react-native run-android and react-native run-ios. Leave Metro running in its terminal and build and run the app from a second one.
React Native's defaults set the server port to the RCT_METRO_PORT environment variable, or 8081 when it isn't set. When another process already holds 8081, stop that process or move Metro. To find and stop it on macOS and Linux:
bashsudo lsof -i :8081 kill -9 <PID>
To move Metro instead, run npm start -- --port=8088 or yarn start --port 8088. A --port argument takes precedence over the port in the config. The app also has to load its bundle from the new port. When you run on a device from Xcode, replace each 8081 in ios/<AppName>.xcodeproj/project.pbxproj with your port.
Object or function
metro.config.js can export an object, the recommended form, which Metro merges on top of its default config. It can also export a function that Metro calls with the default config and that returns the final config. The template uses the object form:
jsconst config = {}; module.exports = mergeConfig(getDefaultConfig(__dirname), config);
getDefaultConfig(__dirname) returns a complete config built by merging Metro's own default values, then React Native's defaults, then any framework defaults. A Metro config for React Native must extend React Native's layer (an Expo project extends @expo/metro-config instead). That layer sets the resolver platforms to android and ios, uses @react-native/metro-babel-transformer as the Babel transformer, and runs react-native/setup-env, the module that sets up global timers and the global console, before your main module.
A config function looks like this:
jsmodule.exports = function (baseConfig) { const config = mergeConfig(baseConfig, getDefaultConfig(__dirname)); return mergeConfig(config, {/* your options */}); };
Metro's config loader builds a default config from Metro's own defaults module, plus any overrides the tool loading the config passes in, calls your function with it, and merges what the function returns over that default config. The Metro page of the React Native docs says Metro will not apply any internal defaults when metro.config.js exports a function, but the loader merges the function's result over Metro's default config anyway. baseConfig starts from Metro's own defaults module, not from the getDefaultConfig of @react-native/metro-config. So the function merges getDefaultConfig(__dirname) into baseConfig itself, and React Native's defaults end up in the result whatever baseConfig held.
Since @react-native/metro-config 0.72.1, the complete default config is available without a function. Use the function form when you need to read the base config Metro passes in or set options dynamically.
Adding to a default list without dropping it
mergeConfig merges a section such as resolver one level deep by spreading your section over the default one. An array you set therefore replaces the default array instead of extending it. With resolver: {assetExts: ['lottie']}, lottie would be the only entry in the merged assetExts.
The metro.config.js example reads the default list from getDefaultConfig and spreads it into the new one:
jsassetExts: [...defaultConfig.resolver.assetExts, 'lottie'],
Spreading the default list works and keeps the file short, but the recommended form is to copy the full list into your config and edit it there, so the file shows the final value and is the source of truth for it. Like the template, the example types its config with the JSDoc line @type {import('@react-native/metro-config').MetroConfig}.
Code Examples
const {getDefaultConfig, mergeConfig} = require('@react-native/metro-config');
const defaultConfig = getDefaultConfig(__dirname);
/**
* @type {import('@react-native/metro-config').MetroConfig}
*/
const config = {
resolver: {
// Keep every default asset extension and add one more
assetExts: [...defaultConfig.resolver.assetExts, 'lottie'],
},
};
module.exports = mergeConfig(defaultConfig, config);