This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.
如何使用独立的 Expo 模块
了解如何通过使用 monorepo 或将包发布到 npm,在项目中使用通过 create-expo-module 创建的独立模块。
在现有项目中创建 Expo 模块的推荐方法 已在 Expo Modules API: 入门 指南中描述。本教程解释了在现有项目中使用通过 create-expo-module 创建的模块的另外两种方法:
如果你仍然希望将模块与应用分开或与其他开发者共享,这些方法非常有用。
🌐 These methods are useful if you still want to keep the module separate from the application or share it with other developers.
使用大仓
🌐 Use a monorepo
你的项目应使用以下结构:
🌐 Your project should use the following structure:
- apps:一个用于存储多个项目的目录,包括 React Native 应用。
- packages:一个用于存放你的应用所使用的不同软件包的目录。
- package.json:这是包含 Yarn 工作区配置的根包文件。
信息 要了解如何将你的项目配置为单一代码库,请查看 使用单一代码库 指南。
1
2
设置工作区依赖
🌐 Set up a workspace dependency
将你的本地模块从 packages 添加到应用的依赖中。更新 apps 目录中每个将使用你的本地模块的应用的 package.json 文件,并将本地模块添加到现有的依赖中:
🌐 Add your native module from packages to your apps' dependencies. Update the package.json file in each app inside the apps directory that will use your native module and add your native module to the existing entries of dependencies:
3
运行模块
🌐 Run the module
运行你的某个应用以确保一切正常。然后,在 packages/expo-settings 中启动 TypeScript 编译器,以监视更改并重新构建模块的 JavaScript:
🌐 Run one of your apps to ensure everything works. Then, start the TypeScript compiler in packages/expo-settings to watch for changes and rebuild the module's JavaScript:
打开另一个终端窗口,从 apps 目录中选择一个应用,然后使用 --clean 选项运行 prebuild 命令。对 monorepo 中的每个应用重复这些步骤以使用新模块。
🌐 Open another terminal window, select an app from the apps directory, and run the prebuild command with the --clean option. Repeat these steps for each app in your monorepo to use the new module.
使用以下命令编译并运行应用:
🌐 Compile and run the app with the following command:
你现在可以在你的应用中使用该模块。要进行测试,请编辑你应用中的 src/app/index.tsx 文件,并渲染来自 expo-settings 模块的文本信息:
🌐 You can now use the module in your app. To test it, edit the src/app/index.tsx file in your app and render the text message from the expo-settings module:
完成此配置后,应用会显示文本“Hello world! 👋”。
🌐 After this configuration, the app displays the text "Hello world! 👋".
将模块发布到 npm
🌐 Publish the module to npm
你可以按照以下步骤将模块发布到 npm 并将其作为依赖安装到你的项目中。
🌐 You can publish the module on npm and install it as a dependency in your project by following the steps below.
1
2
运行示例项目
🌐 Run the example project
运行你的一款应用以确保一切正常。然后,在项目根目录启动 TypeScript 编译器,以监视更改并重新构建模块的 JavaScript:
🌐 Run one of your apps to ensure everything works. Then, start the TypeScript compiler in the root of your project to watch for changes and rebuild the module's JavaScript:
打开另一个终端窗口,编译并运行示例应用:
🌐 Open another terminal window, compile and run the example app:
3
将包发布到 npm
🌐 Publish the package to npm
要将你的包发布到 npm,你需要一个 npm 账户。如果你还没有账户,请在 npm 网站 上创建一个账户。创建账户后,通过运行以下命令登录:
🌐 To publish your package to npm, you need an npm account. If you don't have one, create an account on the npm website. After creating an account, log in by running the following command:
导航到你的模块根目录,然后运行以下命令来发布它:
🌐 Navigate to the root of your module, then run the following command to publish it:
你的模块现在将发布到 npm,并可以在其他项目中使用 npm install 安装。
🌐 Your module will now be published to npm and can be installed in other projects using npm install.
除了将你的模块发布到 npm,你还可以在项目中通过以下方式使用它:
🌐 Apart from publishing your module to npm, you can use it in your project in the following ways:
- 创建 tar 包:使用
npm pack创建你的模块的 tar 包,然后通过运行npm install /path/to/tarball将其安装到你的项目中。这种方法有助于在发布模块或与无法访问 npm 注册表的其他人共享之前,在本地测试你的模块。 - 运行本地 npm 注册表:使用像 Verdaccio 这样的工具来托管本地 npm 注册表。你可以从该注册表安装你的模块,这对于管理公司或组织内部的软件包非常有用。
- 发布私有包: 使用 EAS Build 的私有注册表 来安全地管理私有模块。
4
测试已发布的模块
🌐 Test the published module
要在新项目中测试已发布的模块,请创建一个新的应用,并通过运行以下命令将该模块作为依赖安装:
🌐 To test the published module in a new project, create a new app and install the module as a dependency by running the following command:
你现在可以在你的应用中使用该模块了!要测试它,请编辑 src/app/index.tsx 并渲染来自 expo-settings 的文本消息。
🌐 You can now use the module in your app! To test it, edit src/app/index.tsx and render the text message from expo-settings.
最后,通过执行以下命令预构建项目并运行应用:
🌐 Finally, prebuild your project and run the app by executing the following commands:
完成此配置后,你将在应用中看到显示的文字“Hello world! 👋”。
🌐 After this configuration, you see the text "Hello world! 👋" displayed in the app.
下一步
🌐 Next steps
学习如何在 Expo 模块中封装第三方原生库。
关于使用 Expo Modules API 创建保留设置的原生模块的教程。