Skip to main content
Version: Next

pnpm publish

将包发布到注册表。

🌐 Publishes a package to the registry.

pnpm [-r] publish [<tarball|folder>] [--tag <tag>]
[--access <public|restricted>] [options]
note

自 v11 起,pnpm publish 已原生实现,不再委托给 npm CLI。如果你依赖的某个功能现在已经不存在,请在 pnpm/pnpm 提交问题。作为一种变通方法,你仍然可以运行 pnpm pack && npm publish *.tgz

🌐 Since v11, pnpm publish is implemented natively and no longer delegates to the npm CLI. If you rely on a feature that is now gone, please open an issue at pnpm/pnpm. As a workaround, you can still run pnpm pack && npm publish *.tgz.

workspace 内发布包时,工作区根目录下的 LICENSE 文件会随包一起打包(除非该包有自己的许可证)。

🌐 When publishing a package inside a workspace, the LICENSE file from the root of the workspace is packed with the package (unless the package has a license of its own).

你可以在发布前覆盖某些字段,使用 package.json 中的 publishConfig 字段。你也可以使用 publishConfig.directory 来自定义发布的子目录(通常使用第三方构建工具)。

🌐 You may override some fields before publish, using the publishConfig field in package.json. You also can use the publishConfig.directory to customize the published subdirectory (usually using third party build tools).

当递归运行此命令(pnpm -r publish)时,pnpm 将发布所有尚未向注册表发布版本的包。

🌐 When running this command recursively (pnpm -r publish), pnpm will publish all the packages that have versions not yet published to the registry.

选项

🌐 Options

--recursive, -r

发布工作区中的所有包。

🌐 Publish all packages from the workspace.

--json

以 JSON 格式显示信息。

🌐 Show information in JSON format.

--tag <tag>

使用给定的标签发布包。默认情况下,pnpm publish 会更新 latest 标签。

🌐 Publishes the package with the given tag. By default, pnpm publish updates the latest tag.

例如:

🌐 For example:

# inside the foo package directory
pnpm publish --tag next
# in a project where you want to use the next version of foo
pnpm add foo@next

--access <public|restricted>

告诉注册中心发布的包应该是公开的还是受限制的。

🌐 Tells the registry whether the published package should be public or restricted.

--no-git-checks

不要检查当前分支是否是你的发布分支、干净且与远程保持同步。

🌐 Don't check if current branch is your publish branch, clean, and up-to-date with remote.

--publish-branch <branch>

  • 默认: mastermain
  • 类型:字符串

仓库的主要分支,用于发布最新的更改。

🌐 The primary branch of the repository which is used for publishing the latest changes.

--force

即使当前版本已在注册表中找到,也要尝试发布软件包。

🌐 Try to publish packages even if their current version is already found in the registry.

--batch

新增于:v11.7.0

🌐 Added in: v11.7.0

在递归发布 (pnpm -r publish) 时,将所有选定的包在单个 PUT /-/pnpm/v1/publish 请求中发送到注册表,而不是每个包发送一个请求。

🌐 When publishing recursively (pnpm -r publish), send all selected packages to the registry in a single PUT /-/pnpm/v1/publish request instead of one request per package.

目标注册表必须实现批量发布端点(pnpr 已实现);未实现的注册表会报告 ERR_PNPM_BATCH_PUBLISH_UNSUPPORTED 错误。批量处理是全部或无:如果批次中的任何软件包验证失败,则没有任何软件包会被发布。

🌐 The target registry has to implement the batch publish endpoint (pnpr does); registries that don't are reported with an ERR_PNPM_BATCH_PUBLISH_UNSUPPORTED error. The batch is processed all-or-nothing: if any package in the batch fails validation, none of the packages are published.

--skip-manifest-obfuscation

新增于:v11.3.0

🌐 Added in: v11.3.0

保留原始的 packageManager 字段,并在发布的清单中发布生命周期脚本,而不是将其剥离。特定于 pnpm 的 pnpm 字段仍然被省略。

🌐 Keep the original packageManager field and publish lifecycle scripts in the published manifest instead of stripping them. The pnpm-specific pnpm field is still omitted.

--report-summary

将已发布的软件包列表保存到 pnpm-publish-summary.json。当使用其他工具来报告已发布软件包列表时,这非常有用。

🌐 Save the list of published packages to pnpm-publish-summary.json. Useful when some other tooling is used to report the list of published packages.

pnpm-publish-summary.json 文件的一个示例:

🌐 An example of a pnpm-publish-summary.json file:

{
"publishedPackages": [
{
"name": "foo",
"version": "1.0.0"
},
{
"name": "bar",
"version": "2.0.0"
}
]
}

--dry-run

除了实际发布到注册表之外,执行发布会执行的所有操作。

🌐 Does everything a publish would do except actually publishing to the registry.

--otp

当发布需要双重身份验证的包时,此选项可以指定一次性密码。

🌐 When publishing packages that require two-factor authentication, this option can specify a one-time password.

你也可以通过 PNPM_CONFIG_OTP 环境变量提供 OTP:

🌐 You can also provide the OTP via the PNPM_CONFIG_OTP environment variable:

export PNPM_CONFIG_OTP='<your OTP here>'
pnpm publish --no-git-checks

如果注册表请求 OTP 而你尚未通过环境变量或 --otp 标志提供它,pnpm 将直接提示你输入 OTP 代码。

🌐 If the registry requests OTP and you have not provided it via the environment variable or the --otp flag, pnpm will prompt you directly for an OTP code.

如果注册表请求基于网络的身份验证,pnpm 将打印一个可扫描的二维码以及 URL。

🌐 If the registry requests web-based authentication, pnpm will print a scannable QR code along with the URL.

--provenance

从受支持的云 CI/CD 系统发布时,包将公开链接到其构建和发布的位置。

🌐 When publishing from a supported cloud CI/CD system, the package will be publicly linked to where it was built and published from.

--filter <package_selector>

了解更多关于筛选的信息。

配置

🌐 Configuration

你还可以在 pnpm-workspace.yaml 文件中设置 gitCheckspublishBranch 选项。

🌐 You can also set gitChecks, publishBranch options in the pnpm-workspace.yaml file.

例如:

🌐 For example:

pnpm-workspace.yaml
gitChecks: false
publishBranch: production

生命周期脚本

🌐 Life Cycle Scripts

  • prepublishOnly
  • prepublish
  • prepack
  • prepare
  • postpack
  • publish
  • postpublish