用于更安全自动化的仅暂存 npm 令牌
英文标题:Stage-only npm tokens for safer automation
npm 现已支持在创建细粒度访问令牌时选择“读取和写入(仅暂存)”权限,使自动化工作流能够提交待审核的软件包版本,而无法直接发布到 npm 注册表。工作流需使用 `npm stage publish` 提交版本,并由软件包维护者通过双因素认证(2FA)审核并批准发布。该令牌仍保留移动 dist-tag、弃用版本等其他软件包写入权限,因此仍需按写入令牌的安全标准妥善保护。npm 计划于 2027 年 1 月取消通过绕过 2FA 令牌进行直接发布;对于暂时无法采用 trusted publishing 的团队,仅暂存令牌可作为基于令牌自动化的迁移路径。
128.0 KB
中文
现在,创建 npm 细粒度访问令牌时可以选择 读取和写入(仅暂存)。这使你的自动化工作流能够暂存软件包版本以供审核,而无需授予该令牌直接将新版本发布到 npm 注册表的权限。
你的工作流使用 npm stage publish 提交一个版本。随后,软件包维护者通过双因素认证(2FA)审核并批准其发布。npm 会拒绝使用该令牌直接执行 npm publish 的尝试,即使你已将其配置为允许自动化绕过 2FA。
仅暂存令牌仍保留其他软件包写入权限,包括移动 dist-tag 和弃用版本。请以保护任何其他写入令牌的同等谨慎程度保护它们。
此次发布采用自愿启用方式,不会改变现有令牌或其直接发布能力。
如先前所宣布,npm 的目标是在 2027 年 1 月取消通过绕过 2FA 令牌进行直接发布。如果你暂时无法迁移至 trusted publishing,仅暂存令牌为基于令牌的自动化提供了一条迁移路径。
开始使用:
- 为工作流所需的软件包创建一个细粒度访问令牌,并授予 读取和写入(仅暂存) 权限。
- 替换工作流的发布令牌,并使用
npm stage publish代替npm publish。 - 由维护者通过 2FA 审核并批准已暂存的版本。
暂存发布适用于现有 npm 软件包。你需要拥有该软件包的发布权限、在 npm 账户中启用 2FA、使用 npm CLI 11.15.0 或更高版本,以及 Node.js 22.14.0 或更高版本。
进一步了解暂存发布,并可在 npm 社区讨论分类中分享问题或迁移阻碍。
English / 原文
You can now select Read and write (stage only) when creating an npm granular access token. This lets your automated workflows stage package versions for review without giving the token permission to publish new versions directly to the npm registry.
Your workflow uses npm stage publish to submit a version. A package maintainer then reviews and approves its release with two-factor authentication (2FA). npm rejects direct npm publish attempts with that token, even if you’ve configured it to bypass 2FA for automation.
Stage-only tokens retain other package write permissions , including moving dist-tags and deprecating versions. Protect them with the same care as any other write token.
This release is opt-in and doesn’t change existing tokens or their direct-publish capabilities.
As previously announced, npm is targeting January 2027 to remove direct publishing through bypass-2FA tokens. If you can’t move to trusted publishing yet, stage-only tokens offer a migration path for token-based automation.
To get started:
- Create a granular access token with Read and write (stage only) permissions for the packages your workflow needs.
- Replace your workflow’s publishing token and use
npm stage publishinstead ofnpm publish. - Have a maintainer review and approve staged versions with 2FA.
Staged publishing works with existing npm packages. You’ll need publish access to the package, 2FA enabled on your npm account, npm CLI 11.15.0 or later, and Node.js 22.14.0 or later.
Learn more about staged publishing, and share questions or migration blockers in the npm community discussion category.