1---
2title: Integrate in an existing library
3description: Learn how to integrate Expo Modules API into an existing React Native library.
4---
5
6import { CodeBlocksTable } from '~/components/plugins/CodeBlocksTable';
7
8There are cases where you may want to integrate the Expo Modules API into an existing React Native library. For example, this may be useful to incrementally rewrite the library or to take advantage of [iOS AppDelegate subscribers](/modules/appdelegate-subscribers/) and [Android Lifecycle listeners](/modules/android-lifecycle-listeners/) to automatically set up your library.
9
10The following steps will set up your existing React Native library to access Expo Modules APIs.
11
12Create the [**expo-module.config.json**](./module-config.mdx) file at the root of your project and start from the empty object `{}` inside it. We will fill it in later to enable specific features.
13The presence of the module config is enough for [Expo Autolinking](./autolinking.mdx) to recognize it as an Expo module and automatically link your native code.
14
15### Add the `expo-modules-core` native dependency
16
17Add `expo-modules-core` as a dependency in your podspec and **build.gradle** files.<br/>
18
19<CodeBlocksTable tabs={["*.podspec", "build.gradle"]}>
20
21```ruby
22# ...
23Pod::Spec.new do |s|
24  # ...
25  s.dependency 'ExpoModulesCore'
26end
27```
28
29```groovy
30// ...
31dependencies {
32  // ...
33  implementation project(':expo-modules-core')
34}
35```
36
37</CodeBlocksTable>
38
39### Add Expo packages to dependencies
40
41Add `expo` package as a peer dependency in your **package.json** — we recommend using `*` as a version range so as not to cause any duplicated packages in user's **node_modules** folder. Your library also needs to depend on `expo-modules-core` but only as a dev dependency — it's already provided in the projects depending on your library by the `expo` package with the version of core that is compatible with the specific SDK used in the project.<br/>
42
43```json package.json
44{
45  // ...
46  "devDependencies": {
47    "expo-modules-core": "^X.Y.Z"
48  },
49  "peerDependencies": {
50    "expo": "*"
51  }
52}
53```
54
55### Create a native module
56
57To use the Expo Modules API for native modules, you need to [set up your library as an Expo module](#set-up-a-library-as-an-expo-module). Once complete, create Swift and Kotlin files from the templates below.
58
59<CodeBlocksTable tabs={["MyModule.swift", "MyModule.kt"]}>
60
61```swift
62import ExpoModulesCore
63
64public class MyModule: Module {
65  public func definition() -> ModuleDefinition {
66    // Definition components go here
67  }
68}
69```
70
71```kotlin
72package my.module.package
73
74import expo.modules.kotlin.modules.Module
75import expo.modules.kotlin.modules.ModuleDefinition
76
77class MyModule : Module() {
78  override fun definition() = ModuleDefinition {
79    // Definition components go here
80  }
81}
82```
83
84</CodeBlocksTable>
85
86Then, add your classes to Android and/or iOS `modules` in the [module config](./module-config.mdx). Expo Autolinking will automatically link these classes as native modules in the user's project.
87
88```json expo-module.config.json
89{
90  "ios": {
91    "modules": ["MyModule"]
92  },
93  "android": {
94    "modules": ["my.module.package.MyModule"]
95  }
96}
97```
98
99If you already have an example app in your workspace, ensure that the module is linked correctly.
100
101- **On Android** the native module class will be linked automatically before building, as part of the Gradle build task.
102- **On iOS** you need to run `pod install` to link the new class.
103  These module classes can now be accessed from the JavaScript code using the `requireNativeModule` function from the `expo-modules-core` package. We recommend creating a separate file that exports the native module for simplicity.
104
105```ts MyModule.ts
106import { requireNativeModule } from 'expo-modules-core';
107
108export default requireNativeModule('MyModule');
109```
110
111Now that the class is set up and linked, you can start to implement its functionality. See the [native module API](./module-api.mdx) reference page and links to [examples](./module-api.mdx#examples) from simple to moderately complex real-world modules for your reference to better understand how to use the API.
112