注意: 要使用作用域,你必须使用 npm 版本 2 或更高版本。要升级到最新版本的 npm,请在命令行中运行 npm install npm@latest -g

当你注册 npm 用户账户或创建一个组织时,你将获得一个与你的用户或组织名称匹配的作用域。你可以将此作用域作为相关包的命名空间使用。

🌐 When you sign up for an npm user account or create an organization, you are granted a scope that matches your user or organization name. You can use this scope as a namespace for related packages.

范围允许你创建与其他用户或组织创建的包同名的包,而不会发生冲突。

🌐 A scope allows you to create a package with the same name as a package created by another user or organization without conflict.

当在 package.json 文件中列为依赖时,作用域包前会加上其作用域名称。作用域名称是 @ 和斜杠之间的所有内容:

🌐 When listed as a dependent in a package.json file, scoped packages are preceded by their scope name. The scope name is everything between the @ and the slash:

  • “npm” 范围:
@npm/package-name
  • “npmcorp” 范围:
@npmcorp/package-name

要创建和发布公共范围的软件包,请参见“创建和发布范围公共包”。

🌐 To create and publish public scoped packages, see "Creating and publishing scoped public packages".

要创建和发布私有范围的包,请参阅“创建和发布私有包”。

🌐 To create and publish private scoped packages, see "Creating and publishing private packages".

范围和包可见性

🌐 Scopes and package visibility

  • 无范围的包总是公开的。
  • 私有软件包 总是有作用域。
  • 带范围的包默认是私有的;发布时必须使用命令行标志才能将其设为公开。

有关包的作用域和可见性的更多信息,请参见“包作用域、访问级别和可见性”。

🌐 For more information on package scope and visibility, see "Package scope, access level, and visibility".