Edit this page
Learn how to use Expo Autolinking to automatically link native dependencies in your Expo project.
Usually, when you're developing a native mobile app and want to install a third-party library, you're asked to add the dependency to the manifest files of your package managers (build.gradle on Android, Podfile for CocoaPods on iOS, Package.swift for SwiftPM on iOS). In Expo and React Native, you already do that with your package.json file by installing the package from the npm registry. Since most of the React Native libraries come with some native (platform-specific) code, installing a library will require configuring even up to three different package managers!
Expo Autolinking is a mechanism that automates this process and reduces the library installation process to the minimum — usually just installing the package from npm
and re-running pod install
.
The core implementation can be found in the expo-modules-autolinking
package and is divided into three parts:
search
Searching is the first phase of resolving the Expo modules installed in a project. Its implementation is shared between all platforms. It finds all modules marked as Expo modules and determines which version is of the highest precedence (in case of duplicates).
-
npx expo-modules-autolinking search
The above command returns an object in JSON format with modules that have been found as dependencies:
{
"expo-random": {
"path": "/absolute/path/to/node_modules/expo-random",
"version": "13.0.0",
"config": {
// Contents of `expo-module.config.json`
"platforms": ["ios", "android"],
"ios": { "modules": ["RandomModule"] },
"android": { "modules": ["expo.modules.random.RandomModule"] }
},
"duplicates": [] // An array of other revisions (with lower precedence) of the same module
}
// more modules...
}
resolve
Resolving is the second phase based on the results from the search
command. It resolves each search result to an object with more (platform-specific) details, such as the path to the podspec or build.gradle files and module classes to link.
-
npx expo-modules-autolinking resolve --platform <apple|android>
For example, with the --platform apple
option it returns an object in JSON format with an array of modules and resolved details for the platform:
{
"modules": [
{
"packageName": "expo-random",
"packageVersion": "13.0.0",
"pods": [
{
"podName": "ExpoRandom",
"podspecDir": "/absolute/path/to/node_modules/expo-random/ios"
}
],
"swiftModuleNames": ["ExpoRandom"],
"modules": ["RandomModule"],
"appDelegateSubscribers": [],
"reactDelegateHandlers": [],
"debugOnly": false
}
// more modules...
]
}
verify
Verifies the search results by checking whether there are no duplicate packages, otherwise an appropriate warning is shown.
-
npx expo-modules-autolinking verify
The behavior of the module resolution can be customized using some configuration options. These options can be defined in three different places, from the lowest to the highest precedence:
expo.autolinking
config object in application's package.jsonexpo.autolinking.ios
and expo.autolinking.android
objectsuse_expo_modules!
method in the Podfile or useExpoModules
function in the settings.gradlesearchPaths
A list of paths relative to the app's root directory where the autolinking script should search for Expo modules. It defaults to a list of all node_modules folders found when traversing up through a monorepo, starting from the app's root directory. Useful when your project has a custom structure or you want to link local packages from folders different than node_modules.
{
"expo": {
"autolinking": {
"searchPaths": ["../../packages"]
}
}
}
When used with the CLI, you can pass the search paths as command arguments like this:
-
npx expo-modules-autolinking search ../../packages
exclude
A list of package names to exclude from autolinking. These packages will not be autolinked even if they are found in the search paths.
For example, you may want not to link some packages that you don't use on the specific platform to reduce the binary size.
The following config in package.json will exclude expo-random
from autolinking on Android:
{
"expo": {
"autolinking": {
"android": {
"exclude": ["expo-random"]
}
}
}
}
Note that the exclude
option is for excluding the autolinking of Expo packages. To exclude the autolinking for any other packages, create react-native.config.js at the root directory of your project and apply the configuration as shown in the example below:
module.exports = {
dependencies: {
'library-name': {
platforms: {
android: null,
},
},
},
};
flags
CocoaPods flags to pass to each autolinked pod. inhibit_warnings
is likely the only flag most developers want to use, to inhibit Xcode warnings produced when compiling the autolinked modules.
You can refer to the CocoaPods Podfile documentation for available flags.
use_expo_modules!({
flags: {
:inhibit_warnings => false
}
})
{
"expo": {
"autolinking": {
"ios": {
"flags": {
"inhibit_warnings": true
}
}
}
}
}
All projects created with the npx create-expo-app
command are already configured to use Expo Autolinking. If your project was created using a different tool, see Installing Expo modules to make sure your project includes all necessary changes.
The module resolution algorithm searches only for packages that contain the Expo module config file (expo-module.config.json) at the root directory, next to the package.json file.
It's also necessary to include supported platforms in the platforms
array — if the platform for which the autolinking algorithm is run is not present in this array, it's just skipped in the search results.
Starting from SDK 52, Expo Autolinking comes enabled by default, if you would like to use the React Native community CLI autolinking instead, set EXPO_USE_COMMUNITY_AUTOLINKING=1
and add @react-native-community/cli
as a dev dependency to your project.