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 从公开仓库中收录这些内容。

举报或申请下架