全部学科
Python全栈
python
NodeJS全栈
nodejs
📅 2026-05-25 8 分钟 ✍️ juanwangdev

scoped包与权限标签管理

scoped包解决命名冲突,access控制可见性,dist-tag管理发布通道,是团队包管理的核心机制。

scoped包命名空间

基本格式

JSON
{
  "name": "@myorg/ui-components",
  "version": "1.0.0"
}

scope为@myorg,包名为ui-components,全称为@myorg/ui-components

安装与引用

Bash
npm install @myorg/ui-components
JavaScript
const components = require('@myorg/ui-components');

发布

Bash
# scoped包默认发布为私有包(需付费)
# 发布为公开包
npm publish --access public

# 首次发布后永久设置
npm config set @myorg:access public

初始化scope

Bash
npm init --scope=myorg

免费npm账号只能发布公开scoped包,私有scoped包需npm付费计划或使用私有registry。

npm access权限管理

包可见性设置

Bash
# 设置为公开包
npm access public @myorg/ui-components

# 设置为私有包
npm access restricted @myorg/ui-components

团队权限

Bash
# 给团队授予只读权限
npm access grant read-only myorg:dev-team @myorg/ui-components

# 给团队授予读写权限
npm access grant read-write myorg:dev-team @myorg/ui-components

# 撤销权限
npm access revoke myorg:dev-team @myorg/ui-components

查看权限

Bash
# 查看包的权限列表
npm access ls-packages myorg

# 查看团队对包的权限
npm access ls-collaborators @myorg/ui-components

2FA要求

Bash
# 要求发布时启用两步验证
npm access 2fa-required @myorg/ui-components

# 取消要求
npm access 2fa-not-required @myorg/ui-components

新建的scoped包默认为restricted(私有),首次发布时需明确指定--access public。

npm dist-tag分发标签

默认标签

npm内置三个标签:

标签含义npm install默认
latest稳定版
next下一版本候选
beta测试版

自定义标签发布

Bash
# 发布beta版本
npm publish --tag beta

# 发布next版本
npm publish --tag next

# 发布rc版本
npm publish --tag rc

安装指定标签

Bash
# 安装beta版本
npm install my-lib@beta

# 安装next版本
npm install my-lib@next

标签管理

Bash
# 查看所有标签
npm dist-tag ls my-lib

# 添加标签到指定版本
npm dist-tag add my-lib@2.0.0 stable

# 删除标签
npm dist-tag rm my-lib old-tag

npm install默认安装latest标签对应的版本。发布非稳定版本时务必指定--tag,避免用户意外安装到不稳定的包。

要点总结

  • scoped包格式为@scope/name,避免命名冲突,默认私有
  • npm publish --access public将scoped包发布为公开包
  • npm access管理团队读写权限和2FA要求
  • dist-tag将版本标记为beta/next/rc等通道
  • npm install默认安装latest标签,非稳定版本需指定--tag发布
想在手机上练习这篇文章的配套题目?
使用微信卷王开发者小程序,打开首页顶部扫码功能识别二维码
← 上一篇 npm link与发布内容控制
下一篇 → NPM镜像源与私有仓库配置
扫码体验小程序
加载中
想在手机上刷题学习?
使用微信卷王开发者小程序,打开首页顶部扫码功能识别二维码