xref: /expo/docs/pages/modules/autolinking.mdx (revision 44fca1ee)
1---
2title: Autolinking
3---
4
5import { Terminal } from '~/ui/components/Snippet';
6import { APIBox } from '~/components/plugins/APIBox';
7import { CodeBlocksTable } from '~/components/plugins/CodeBlocksTable';
8
9Usually, 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).
10In Expo and React Native, you already do that with your **package.json** file by installing the package from the [npm](https://www.npmjs.com) registry. Since most of the React Native libraries come with some native (platform-specific) code,
11installing a library will require configuring even up to three different package managers!
12
13Expo 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`.
14The core implementation can be found in the [`expo-modules-autolinking`](https://github.com/expo/expo/tree/main/packages/expo-modules-autolinking) package and is divided into three parts:
151. common JavaScript CLI tool with the module resolution algorithm
162. code that integrates with the Gradle build system for Android platform
173. Ruby script that integrates with CocoaPods for iOS platform
18
19## CLI commands
20
21<APIBox>
22
23### `search`
24
25Searching 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).
26
27<Terminal cmd={['$ npx expo-modules-autolinking search']} />
28
29The above command returns an object in JSON format with modules that have been found as dependencies:
30
31```json
32{
33  "expo-random": {
34    "path": "/absolute/path/to/node_modules/expo-random",
35    "version": "13.0.0",
36    "config": { // Contents of `expo-module.config.json`
37      "platforms": [ "ios", "android" ],
38      "ios": { "modules": [ "RandomModule" ] },
39      "android": { "modules": [ "expo.modules.random.RandomModule" ] }
40    },
41    "duplicates": [] // An array of other revisions (with lower precedence) of the same module
42  },
43  // more modules...
44}
45```
46
47</APIBox>
48<APIBox>
49
50### `resolve`
51
52Resolving 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.
53
54<Terminal cmd={['$ npx expo-modules-autolinking resolve --platform <ios|android>']} />
55
56For example, with the `--platform ios` option it returns an object in JSON format with an array of modules and resolved details for the iOS platform:
57
58```json
59{
60  "modules": [
61    {
62      "packageName": "expo-random",
63      "packageVersion": "13.0.0",
64      "pods": [
65        {
66          "podName": "ExpoRandom",
67          "podspecDir": "/absolute/path/to/node_modules/expo-random/ios"
68        }
69      ],
70      "swiftModuleNames": [ "ExpoRandom" ],
71      "modules": [ "RandomModule" ],
72      "appDelegateSubscribers": [],
73      "reactDelegateHandlers": [],
74      "debugOnly": false
75    },
76    // more modules...
77  ]
78}
79```
80
81</APIBox>
82<APIBox>
83
84### `verify`
85
86Verifies the search results by checking whether there are no duplicate packages, otherwise an appropriate warning is shown.
87
88<Terminal cmd={['$ npx expo-modules-autolinking verify']} />
89
90</APIBox>
91
92## Configuration
93
94The 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:
95- `expo.autolinking` config object in application's **package.json**
96- per platform overrides with `expo.autolinking.ios` and `expo.autolinking.android` objects
97- options provided to the CLI command, the `use_expo_modules!` method in the **Podfile** or `useExpoModules` function in the **settings.gradle**
98
99<APIBox>
100
101### `searchPaths`
102
103A list of paths relative to the app's root directory where the autolinking script should search for Expo modules.
104It defaults to a list of all **node_modules** folders found when traversing up through a monorepo, starting from the app's root directory.
105Useful when your project has a custom structure or you want to link local packages from folders different than **node_modules**.
106
107```json package.json
108{
109  "expo": {
110    "autolinking": {
111      "searchPaths": ["../../packages"]
112    }
113  }
114}
115```
116
117When used with the CLI, you can pass the search paths as command arguments like this:
118
119<Terminal cmd={['$ npx expo-modules-autolinking search ../../packages']} />
120
121</APIBox>
122<APIBox>
123
124### `exclude`
125
126A list of package names to exclude from autolinking. These packages will not be autolinked even if they are found in the search paths.
127For example, you may want not to link some packages that you don't use on the specific platform to reduce the binary size.
128The following config in **package.json** will exclude `expo-random` from autolinking on Android:
129
130```json package.json
131{
132  "expo": {
133    "autolinking": {
134      "android": {
135        "exclude": ["expo-random"]
136      }
137    }
138  }
139}
140```
141
142</APIBox>
143<APIBox platforms={['ios']}>
144
145### `flags`
146
147CocoaPods 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.
148You can refer to the [CocoaPods Podfile documentation](https://guides.cocoapods.org/syntax/podfile.html#pod) for available flags.
149
150<CodeBlocksTable tabs={['Podfile', 'package.json']}>
151
152```ruby
153use_expo_modules!({
154  flags: {
155    :inhibit_warnings => false
156  }
157})
158```
159
160```json
161{
162  "expo": {
163    "autolinking": {
164      "ios": {
165        "flags": {
166          "inhibit_warnings": true
167        }
168      }
169    }
170  }
171}
172```
173
174</CodeBlocksTable>
175
176</APIBox>
177
178## Common questions
179
180### How to set up the autolinking in my app?
181
182All projects created with the `npx create-expo-app` command are already configured to use Expo Autolinking. If your project was created using different tools, please refer to [Installing Expo modules](/bare/installing-expo-modules) to make sure your project includes all necessary changes.
183
184### What do I need to have in my module to make it autolinkable?
185
186The module resolution algorithm searches only for packages that contain the [Expo module config](./module-config.mdx) file (**expo-module.config.json**) at the root directory, next to the **package.json** file.
187It'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.
188
189### How is it different from React Native autolinking?
190
191- Expo Autolinking comes in with built-in support for monorepos, Yarn workspaces and transitive dependencies.
192- It's also significantly faster, even though the module resolution algorithm is more complex in order to be more reliable and match the Node's module resolution.
193- Expo module resolution is also capable of detecting the duplicates which is a common issue in monorepos.
194- Last but not least, it integrates well with all the features offered by Expo Modules APIs.
195