Module Resolution
Module resolution is the process of translating module names to module paths at build time. For example, if your project contains the code:
// src/App.js
import {View} from 'react-native';
// ...
Metro needs to know where in your project to load the react-native module from. This will typically resolve to something like node_modules/react-native/index.js.
Likewise, if your project contains the (similar) code:
// src/App.js
import Comp from './Component';
// ...
Metro needs to understand that you are referring to, say, src/Component.js, and not another file named Component that may also exist elsewhere.
Metro implements a version of Node's module resolution algorithm, augmented with additional Metro-specific features.
These Metro-specific features include:
- Haste: An opt-in mechanism for importing modules by their globally-unique name anywhere in the project, e.g.
import Foo from 'Foo'. - Platform extensions: Used by React Native to allow developers to write platform-specific versions of their JavaScript modules.
- Asset extensions and image resolutions: Used by React Native to automatically select the best version of an image asset based on the device's screen density at runtime.
- Custom resolvers: Metro integrators can provide their own resolver implementations to override almost everything about how modules are resolved.
Resolution algorithm
Given a resolution context context, a module name moduleName, and an optional platform identifier platform, Metro's resolver performs RESOLVE(context, moduleName, platform), which either returns one of the resolution types, or throws an error.
Resolution types
Source file
The request is resolved to some absolute path representing a physical file on disk.