了解如何配置 EAS Build 以使用私有 npm 包。
EAS Build 完全支持在项目中使用私有 npm 包。这些可以发布到 npm(如果你有 专业/团队计划)或私有注册表(例如,使用自托管 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.
¥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
¥Private packages published to npm
如果你的项目使用发布到 npm 的私有包,你需要提供带有 只读 npm 令牌 的 EAS Build,以便它可以成功安装你的依赖。
¥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:
//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:
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:
//registry.johndoe.com/:_authToken=${NPM_TOKEN}
registry=https://registry.johndoe.com/
¥Both private npm packages and private registry
这是一个高级示例。
¥This is an advanced example.
私有 npm 包始终是 scoped。例如,如果你的 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:
//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.