NPM 注册表身份验证指南 | AI生成和翻译

Home 2026.02

是的,你可以使用 npm config set 来配置 npm registry 的身份验证。以下是它的工作原理:

Basic Usage

# 用于 _authToken (推荐)
npm config set //registry.npmjs.org/:_authToken "your-token-here"

# 用于 _auth (旧版)
npm config set //registry.npmjs.org/:_auth "base64-encoded-credentials"

注意语法:它是 //registry.org/:_auth(在 _auth 前有一个冒号),而不是 //registry.org/_auth

Difference between _auth and _authToken

_authToken (现代方法):

_auth (旧版方法):

Practical Example

对于像 GitHub Packages 这样的私有 registry:

npm config set //npm.pkg.github.com/:_authToken "ghp_yourGitHubToken"

对于 scoped packages,你可能还需要:

npm config set @yourscope:registry https://npm.pkg.github.com

大多数现代工作流使用 _authToken,因为它更简单且适用于基于 token 的身份验证系统。_auth 方法主要用于仍在使用用户名/密码验证的旧系统。


Back Donate