expo-module

作者: expo

使用Expo Modules API(Swift、Kotlin、TypeScript)创建和编写Expo原生模块与视图的指南。涵盖模块定义DSL、原生…

npx skills add https://github.com/expo/skills --skill expo-module

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

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 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):

import ExpoModulesCore

public class MyModule: Module {
  public func definition() -> ModuleDefinition {
    Name("MyModule")

    Function("hello") { (name: String) -> String in
      return "Hello \(name)!"
    }
  }
}

Kotlin (Android):

package expo.modules.mymodule

import expo.modules.kotlin.modules.Module
import expo.modules.kotlin.modules.ModuleDefinition

class MyModule : Module() {
  override fun definition() = ModuleDefinition {
    Name("MyModule")

    Function("hello") { name: String ->
      "Hello $name!"
    }
  }
}

TypeScript:

import { requireNativeModule } from "expo";

const MyModule = requireNativeModule("MyModule");

export function hello(name: string): string {
  return MyModule.hello(name);
}

expo-module.config.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.

来自 expo 的更多技能

android-e2e-testing
expo
在Android模拟器上使用ADB测试Expo Router功能。在实现原生Android功能后或验证Android上的UI行为时使用。
official
deep-code-review
expo
深度聚焦设计的代码审查——在评估PR变更前理解代码库上下文,并将结构化反馈发布至GitHub
official
building-native-ui
expo
构建原生Expo应用的完整指南,涵盖路由、样式、组件及平台规范。包含Expo Router基础、原生标签页、堆栈导航、模态框和表单页面的详细路由结构约定。遵循苹果人机界面指南的样式规则:弹性盒布局、安全区域处理、动画效果、通过CSS boxShadow实现的阴影,以及响应式设计模式。记录库偏好(expo-image用于SF Symbols、expo-audio、expo-video等)。
official
eas-update-insights
expo
检查已发布EAS更新的健康状况:崩溃率、安装/启动次数、独立用户数、负载大小,以及嵌入式与OTA用户之间的分布情况…
official
expo-api-routes
expo
Expo Router中的API路由,用于在EAS Hosting上处理服务端逻辑、密钥和第三方集成。在app目录中创建以+api.ts后缀的路由;导出命名函数以处理HTTP方法(GET、POST、PUT、DELETE)。处理查询参数、请求头、JSON体和动态路由段;为Web客户端添加CORS头。通过process.env访问服务端密钥;在本地.env文件中设置变量,或通过eas env:create为生产环境设置。使用eas部署到EAS Hosting(Cloudflare Workers)...
official
expo-cicd-workflows
expo
为Expo项目编写并验证EAS CI/CD工作流YAML文件。从Expo API获取最新JSON模式,确保作业类型、参数、触发器和运行器配置均为最新。支持使用${{ }}语法结合GitHub事件、工作流输入、作业输出和步骤结果的上下文动态表达式。包含内置验证脚本,可对照模式检查工作流结构并在部署前报告错误。提供语法参考文档...
official
expo-deployment
expo
自动将Expo应用部署至iOS App Store、Android Play Store、Web托管及预览环境。支持通过单条命令完成iOS(App Store和TestFlight)及Android(Google Play Store)的生产构建与提交。包含EAS Hosting用于Web部署,自动生成PR预览URL并支持生产域名。通过EAS Workflows实现CI/CD工作流自动化,在代码推送时触发构建与提交。自动版本管理支持远程...
official
expo-dev-client
expo
通过EAS Build或在本地构建自定义Expo开发客户端,用于在物理设备上测试原生代码。仅在使用自定义原生模块、Apple目标(小组件、App Clips)或Expo Go中不包含的第三方原生代码时需要;建议先用npx expo start尝试Expo Go。支持云端构建并自动提交至TestFlight,或在本地机器上构建,输出.ipa(iOS)或.apk/.aab(Android)文件。需在eas.json中配置包含development profile的设置...
official