使用 Bun
有关将 Bun 与 Expo 和 EAS 结合使用的指南。
For the complete documentation index, see llms.txt. Use this file to discover all available pages.
Bun 是一个 JavaScript 运行时,也是 Node.js 的替代方案。在 Expo 项目中,Bun 可用于安装 npm 包和运行 Node.js 脚本。使用 Bun 的好处是比 npm、pnpm 或 Yarn 安装包更快,启动时间至少比 Node.js 快 4 倍,这大大提升了本地开发体验。
2 requirements
2 requirements
1.
安装 Bun 来创建一个新应用。
2.
仍然需要一个 Node.js (LTS) 版本 来执行 bun create expo 和 bun expo prebuild 命令,这些命令使用 npm pack 下载项目模板。
与 Bun 一起启动一个新的 Expo 项目
🌐 Start a new Expo project with Bun
要创建新项目,请运行以下命令:
🌐 To create a new project, run the command:
- bun create expo-app my-app你也可以使用 bun run 运行任何 package.json 脚本:
🌐 You can also run any package.json script with bun run:
- bun run ios要安装任何 Expo 库,你可以使用 bun expo install:
🌐 To install any Expo library, you can use bun expo install:
- bun expo install expo-audio使用 Bun 进行 EAS 构建
🌐 Use Bun for EAS builds
EAS 会根据你代码库中的锁文件决定使用哪个包管理器。如果你希望 EAS 使用 Bun,请在你的代码库中运行 bun install。这将在 Bun 1.2 及更新版本中创建一个 Bun 锁文件:bun.lock,或在使用较旧版本的 Bun 时创建 bun.lockb。只要你的代码库中存在其中一个锁文件,Bun 就会被用作构建的包管理器。请确保删除由其他包管理器生成的任何锁文件。
🌐 EAS decides which package manager to use based on the lockfile in your codebase. If you want EAS to use Bun, run bun install in your codebase. This will create a Bun lockfile: bun.lock in Bun versions 1.2 and newer, or bun.lockb when using an older Bun. As long as one of these lockfiles is in your codebase, Bun will be used as the package manager for your builds. Make sure to delete any lockfiles generated by other package managers.
在 EAS 上自定义 Bun 版本
🌐 Customize Bun version on EAS
使用 EAS 时,Bun 会默认安装。请参阅 Android 服务器镜像 和 iOS 服务器镜像 以了解你的构建镜像使用的 Bun 版本。
🌐 Bun is installed by default when using EAS. See the Android server images and iOS server images to learn which version of Bun is used by your build's image.
要在 EAS 中使用 Bun 的精确版本,请在 eas.json 的构建配置文件中添加版本号。例如,下面的配置为 development 构建配置文件指定了 Bun 版本 1.0.0:
🌐 To use an exact version of Bun with EAS, add the version number in eas.json under the build profile's configuration. For example, the configuration below specifies Bun version 1.0.0 for the development build profile:
{ "build": { "development": { "bun": "1.0.0" %%placeholder-start%%... %%placeholder-end%% } %%placeholder-start%%... %%placeholder-end%% } }
可信依赖
🌐 Trusted dependencies
与其他包管理器不同,Bun 不会自动执行已安装库的生命周期脚本,因为这被认为存在安全风险。然而,如果你正在安装的包有一个你想运行的 postinstall 脚本,你必须明确声明这一点,通过在你的 package.json 中的 trustedDependencies 数组中包含该库。
🌐 Unlike other package managers, Bun does not automatically execute lifecycle scripts from installed libraries, as this is considered a security risk. However, if a package you are installing has a postinstall script that you want to run, you have to explicitly state that by including that library in your trustedDependencies array in your package.json.
例如,如果你安装 packageA,它依赖于 packageB,并且 packageB 有一个 postinstall 脚本,你必须在你的 trustedDependencies 中添加 packageB。
🌐 For example, if you install packageA, which has a dependency on packageB and packageB has a postinstall script, you must add packageB in your trustedDependencies.
要在你的 package.json 中添加受信任的依赖,请添加:
🌐 To add a trusted dependency in your package.json, add:
"trustedDependencies": ["your-dependency"]
然后,删除锁定文件并重新安装依赖:
🌐 Then, remove your lockfile and re-install the dependencies:
- rm -rf node_modules- rm bun.lock bun.lockb- bun install常见错误
🌐 Common errors
使用 Sentry 和 Bun 时 EAS 构建失败
🌐 EAS Build fails when using Sentry and Bun
如果你正在使用 sentry-expo 或 @sentry/react-native,它们依赖于 @sentry/cli,该依赖在构建过程中会将源映射更新到 Sentry。@sentry/cli 包含一个 postinstall 脚本,需要运行该脚本,才能使“上传源映射”脚本可用。
🌐 If you're using sentry-expo or @sentry/react-native, these depend on @sentry/cli, which updates source maps to Sentry during your build. The @sentry/cli package has a postinstall script which needs to run for the "upload source maps" script to become available.
要解决此问题,请在 package.json 的 受信任依赖 数组中添加 @sentry/cli:
🌐 To fix this, add @sentry/cli to your trusted dependencies array in package.json:
"trustedDependencies": ["@sentry/cli"]