1---
2title: Integrating 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';
7import { Terminal } from '~/ui/components/Snippet';
8
9There 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.
10
11The following steps will set up your existing React Native library to access Expo Modules APIs.
12
13Create 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.
14The 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.
15
16### Add the `expo-modules-core` native dependency
17
18Add `expo-modules-core` as a dependency in your podspec and **build.gradle** files.<br/>
19
20<CodeBlocksTable tabs={["*.podspec", "build.gradle"]}>
21
22```ruby
23# ...
24Pod::Spec.new do |s|
25  # ...
26  s.dependency 'ExpoModulesCore'
27end
28```
29
30```groovy
31// ...
32dependencies {
33  // ...
34  implementation project(':expo-modules-core')
35}
36```
37
38</CodeBlocksTable>
39
40### Add Expo packages to dependencies
41
42Add `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/>
43
44```json package.json
45{
46  // ...
47  "devDependencies": {
48    "expo-modules-core": "^X.Y.Z"
49  },
50  "peerDependencies": {
51    "expo": "*"
52  }
53}
54```
55
56### Create a native module
57
58To 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.
59
60<CodeBlocksTable tabs={["MyModule.swift", "MyModule.kt"]}>
61
62```swift
63import ExpoModulesCore
64
65public class MyModule: Module {
66  public func definition() -> ModuleDefinition {
67    // Definition components go here
68  }
69}
70```
71
72```kotlin
73package my.module.package
74
75import expo.modules.kotlin.modules.Module
76import expo.modules.kotlin.modules.ModuleDefinition
77
78class MyModule : Module() {
79  override fun definition() = ModuleDefinition {
80    // Definition components go here
81  }
82}
83```
84
85</CodeBlocksTable>
86
87Then, 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.
88
89```json expo-module.config.json
90{
91  "ios": {
92    "modules": ["MyModule"]
93  },
94  "android": {
95    "modules": ["my.module.package.MyModule"]
96  }
97}
98```
99
100If you already have an example app in your workspace, ensure that the module is linked correctly.
101
102- **On Android** the native module class will be linked automatically before building, as part of the Gradle build task.
103- **On iOS** you need to run `pod install` to link the new class.
104  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.
105
106```ts MyModule.ts
107import { requireNativeModule } from 'expo-modules-core';
108
109export default requireNativeModule('MyModule');
110```
111
112Now 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.
113