使用私有 npm 包

了解如何配置 EAS Build 以使用私有 npm 包。


EAS Build 完全支持在你的项目中使用私有 npm 包。这些包可以发布到 npm(如果你有Pro/Teams 计划)或者发布到私有注册表(例如,使用自托管的 Verdaccio)。

🌐 EAS Build has full support for using private npm packages in your project. These can either be published to npm (if you have the Pro/Teams plan) or to a private registry (for example, using self-hosted Verdaccio).

在开始构建之前,你需要配置项目以向 EAS Build 提供你的 npm 令牌。

🌐 Before starting the build, you will need to configure your project to provide EAS Build with your npm token.

默认 npm 配置

🌐 Default npm configuration

默认情况下,EAS Build 使用自托管的 npm 缓存,它可以加快所有构建的依赖安装速度。每个 EAS Build 构建器都为每个平台配置了一个 .npmrc 文件:

🌐 By default, EAS Build uses a self-hosted npm cache that speeds up installing dependencies for all builds. Every EAS Build builder is configured with a .npmrc file for each platform:

安卓

🌐 Android

registry=http://npm-cache-service.worker-infra-production.svc.cluster.local:4873

iOS

registry=http://10.254.24.8:4873

私有包发布到 npm

🌐 Private packages published to npm

如果你的项目使用发布到 npm 的私有包,你需要向 EAS Build 提供只读 npm 令牌,以便它能够成功安装你的依赖。

🌐 If your project is using private packages published to npm, you need to provide EAS Build with a read-only npm token so that it can install your dependencies successfully.

推荐的方式是将 NPM_TOKEN 密钥添加到你的账户或项目的密钥中:

🌐 The recommended way is to add the NPM_TOKEN secret to your account or project's secrets:

有关如何操作的更多信息,请参阅 秘密环境变量

🌐 For more information on how to do that, see secret environment variables.

当 EAS 在构建过程中检测到 NPM_TOKEN 环境变量可用时,它会自动创建以下 .npmrc 文件:

🌐 When EAS detects that the NPM_TOKEN environment variable is available during a build, it automatically creates the following .npmrc:

.npmrc
//registry.npmjs.org/:_authToken=${NPM_TOKEN} registry=https://registry.npmjs.org/

然而,这种情况只会在 .npmrc 不在你项目的根目录时发生。如果你已经有了这个文件,你需要手动更新它。

🌐 However, this only happens when .npmrc is not in your project's root directory. If you already have this file, you need to update it manually.

你可以通过查看构建日志并查找 准备项目 构建阶段来验证它是否成功:

🌐 You can verify if it worked by viewing build logs and looking for the Prepare project build phase:

发布到私有注册表的包

🌐 Packages published to a private registry

如果你正在使用私有 npm 注册表,例如自托管的 Verdaccio,你需要手动配置 .npmrc

🌐 If you're using a private npm registry such as self-hosted Verdaccio, you will need to configure the .npmrc manually.

在项目的根目录下创建一个 .npmrc 文件,内容如下:

🌐 Create a .npmrc file in your project's root directory with the following contents:

.npmrc
registry=__REPLACE_WITH_REGISTRY_URL__

如果你的注册表需要身份验证,你需要提供令牌。例如,如果你的注册表 URL 是 https://registry.johndoe.com/,则更新文件如下:

🌐 If your registry requires authentication, you will need to provide the token. For example, if your registry URL is https://registry.johndoe.com/, then update the file with:

.npmrc
//registry.johndoe.com/:_authToken=${NPM_TOKEN} registry=https://registry.johndoe.com/

私有 npm 包和私有注册表

🌐 Both private npm packages and private registry

这是一个高级示例。

私有 npm 包始终是作用域的。例如,如果你的 npm 用户名是 johndoe,私有自托管注册表的 URL 为 https://registry.johndoe.com/。如果你想从两个来源安装依赖,请在项目根目录下创建一个 .npmrc 文件,内容如下:

🌐 Private npm packages are always scoped. For example, if your npm username is johndoe, the private self-hosted registry URL is https://registry.johndoe.com/. If you want to install dependencies from both sources, create a .npmrc in your project's root directory with the following:

.npmrc
//registry.npmjs.org/:_authToken=${NPM_TOKEN} @johndoe:registry=https://registry.npmjs.org/ registry=https://registry.johndoe.com/

私有存储库中的子模块

🌐 Submodules in private repositories

如果你在私有仓库中有子模块,你需要通过设置 SSH 密钥来初始化它。更多信息,请参阅 子模块初始化

🌐 If you have a submodule in a private repository, you will need to initialize it by setting up an SSH key. For more information, see submodules initialization.