使用 Bun

有关将 Bun 与 Expo 和 EAS 结合使用的指南。


Bun 是一个 JavaScript 运行时,也是 Node.js 的替代方案。在 Expo 项目中,Bun 可用于安装 npm 包和运行 Node.js 脚本。使用 Bun 的好处是比 npm、pnpm 或 Yarn 安装包更快,启动时间至少比 Node.js 快 4 倍,这大大提升了本地开发体验。

先决条件

🌐 Prerequisites

注意: 尽管 Bun 在你的项目中可以替代 Node.js 的大多数使用场景,但目前仍需要安装一个 Node.js LTS 版本 来使用 bun create expobun expo prebuild 命令。这些命令使用 npm pack 来下载项目模板。

要使用 Bun 创建一个新应用,在本地机器上安装 Bun

🌐 To create a new app using Bun, install Bun on your local machine.

与 Bun 一起启动一个新的 Expo 项目

🌐 Start a new Expo project with Bun

要创建新项目,请运行以下命令:

🌐 To create a new project, run the command:

Terminal
bun create expo-app my-app

你也可以使用 bun run 运行任何 package.json 脚本:

🌐 You can also run any package.json script with bun run:

Terminal
bun run ios

要安装任何 Expo 库,你可以使用 bun expo install

🌐 To install any Expo library, you can use bun expo install:

Terminal
bun expo install expo-audio

使用 Bun 进行 EAS 构建

🌐 Use Bun for EAS builds

EAS 会根据代码库中的锁文件决定使用哪个包管理器。如果你希望 EAS 使用 Bun,请在代码库中运行 bun install,这将创建一个 bun.lockb(Bun 锁文件)。只要这个锁文件存在于你的代码库中,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 which will create a bun.lockb (the Bun lockfile). As long as this lockfile 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:

eas.json
{ "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:

package.json
"trustedDependencies": ["your-dependency"]

然后,删除锁定文件并重新安装依赖:

🌐 Then, remove your lockfile and re-install the dependencies:

Terminal
rm -rf node_modules
rm 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:

package.json
"trustedDependencies": ["@sentry/cli"]