Expo Module

作者 expocd752143d19eMIT2.6K 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫昨天更新

Framework (OSS). Guide for creating and writing Expo native modules and views using the Expo Modules API (Swift, Kotlin, TypeScript). Covers module definition DSL, native views, shared objects, config plugins, lifecycle hooks, autolinking, and type system. Use when building or modifying native modules for Expo. Not for migrating an existing Swift module from the definition DSL to the Expo Modules API 2.0 macros; use expo-migrate-module (from the expo-experiments plugin) for that.

AI 產生的概覽

使用 Expo Modules API 以 Swift、Kotlin 和 TypeScript 建置 Expo 原生模組與檢視的參考指南。

功能
此技能提供使用 Expo Modules API 在 Swift、Kotlin 和 TypeScript 中建立與修改 Expo 原生模組及原生檢視的參考資料。內容涵蓋模組定義 DSL、原生檢視、共享物件、設定外掛、生命週期鉤子、自動連結與型別系統,並為每個主題提供對應的參考檔案。它也說明 create-expo-module 與 add-platform-support 等鷹架流程,以及如何設定 expo-module.config.json。
適用情境
適用於建立新的 Expo 原生模組或原生檢視、為 Expo 應用程式加入原生功能或平台 SDK 封裝,或建置會修改原生專案檔案的設定外掛。它也用于為現有模組加入 Android、Apple 或 Web 支援,以及編輯 expo-module.config.json、設定外掛或生命週期鉤子。
執行需求
僅為說明文件,不附帶指令碼。需要 Expo/React Native 專案及原生 iOS(Swift)和/或 Android(Kotlin)工具鏈,並使用 create-expo-module CLI 進行鷹架建立。

Writing Expo Modules

Complete reference for building native modules and views using the Expo Modules API. Covers Swift (iOS), Kotlin (Android), and TypeScript.

When to Use

  • Creating a new Expo native module or native view
  • Adding native functionality (camera, sensors, system APIs) to an Expo app
  • Wrapping platform SDKs for React Native consumption
  • Building config plugins that modify native project files
  • Adding Android, Apple, or web support to an existing Expo module
  • Editing expo-module.config.json, config plugins, or lifecycle hooks

To migrate an existing Swift module from the definition DSL to the Expo Modules API 2.0 macros (@ExpoModule, @JS, @Event), use the expo-migrate-module skill (from the expo-experiments plugin) instead.

References

Consult these resources as needed:

references/  create-expo-module.md      Scaffolding and add-platform-support workflow, defaults, and quirks  native-module.md           Module definition DSL: Name, Function, AsyncFunction, Property, Constant, Events, type system, shared objects  native-view.md             Native view components: View, Prop, EventDispatcher, view lifecycle, ref-based functions  lifecycle.md               Lifecycle hooks: module, iOS app/AppDelegate, Android activity/application listeners  config-plugin.md           Config plugins: modifying Info.plist, AndroidManifest.xml, reading values in native code  module-config.md           expo-module.config.json fields, file placement, and autolinking behavior

Quick Start

Prefer create-expo-module over manually creating native module files and directories. In practice, the best path is usually to create the scaffold first and then build on top of it. The scaffold sets up the expected layout, expo-module.config.json, podspec or Gradle files, TypeScript bindings, and the standalone example app flow.

If an existing Expo module only needs another platform, use create-expo-module add-platform-support instead of manually copying native directories.

See references/create-expo-module.md [blocked] before scaffolding or extending a module. It covers:

  • local vs standalone modules
  • --platform, --features, --barrel, --package-manager, and non-interactive mode
  • expo.autolinking.nativeModulesDir
  • add-platform-support behavior and quirks

Recommended Workflow

  1. Choose the scaffold type first:
    • Local module for one app
    • Standalone module for reuse, monorepos, or publishing
  2. Determine native expo-module features that you will need.
    • Based on the user's instructions determine which feature scaffolding will be useful.
    • Available features: Constant, Function, AsyncFunction, Event, View, ViewEvent, SharedObject
  3. Scaffold deliberately:
    • pass an explicit slug or path
    • choose --platform intentionally instead of relying on defaults
    • use --features to choose code samples which you will modify in the next step to match the real implementation.
  4. Replace generated example code with the real implementation.
  5. If you add a new platform later, prefer add-platform-support over manual file copying.

Practical Scaffolding Rules

  • Feature examples are opt-in. A newly scaffolded module may be minimal if no features were selected.
  • ViewEvent implies View.
  • Local modules do not generate an index.ts barrel by default. Use --barrel only if you want one.
  • In non-interactive local scaffolding, pass the positional slug or path explicitly. --name changes the native class name, not the folder name.
  • Local modules live in expo.autolinking.nativeModulesDir when configured, otherwise in modules/.
  • Standalone modules have their own package metadata, scripts, and usually an example app. Local modules use the host app's tooling instead.

Core File Shapes

The Swift and Kotlin DSL share the same structure. Swift is usually the clearest primary example; consult the references for feature-specific details.

Module Structure Reference

The Swift and Kotlin DSL share the same structure. Both platforms are shown here for reference — in other reference files, Swift is shown as the primary language unless the Kotlin pattern meaningfully differs.

Swift (iOS):

swift
import ExpoModulesCore
public class MyModule: Module {  public func definition() -> ModuleDefinition {    Name("MyModule")
    Function("hello") { (name: String) -> String in      return "Hello \(name)!"    }  }}

Kotlin (Android):

kotlin
package expo.modules.mymodule
import expo.modules.kotlin.modules.Moduleimport expo.modules.kotlin.modules.ModuleDefinition
class MyModule : Module() {  override fun definition() = ModuleDefinition {    Name("MyModule")
    Function("hello") { name: String ->      "Hello $name!"    }  }}

TypeScript:

typescript
import { requireNativeModule } from "expo";
const MyModule = requireNativeModule("MyModule");
export function hello(name: string): string {  return MyModule.hello(name);}

expo-module.config.json

json
{  "platforms": ["android", "apple"],  "apple": {    "modules": ["MyModule"]  },  "android": {    "modules": ["expo.modules.mymodule.MyModule"]  }}

Note: iOS uses just the class name; Android uses the fully-qualified class name (package + class). See references/module-config.md for all fields.

Submitting Feedback

If you encounter errors, misleading or outdated information in this skill, report it so Expo can improve:

bash
npx --yes submit-expo-feedback@latest --category skills --subject "expo-module" "<actionable feedback>"

Only submit when you have something specific and actionable to report. Include as much relevant context as possible. If an AI agent repeatedly failed or the user had to take over an Expo task, load the expo-skill-feedback skill and follow its eval-candidate flow instead of reusing the command above.

來源與署名

來源:expo/skills位於plugins/expo/skills/expo-module提交cd75214

授權條款: MIT

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架