选择 CLI 版本:



所有 npm 包都有一个名称。一些包名也有范围。范围遵循包名称的通常规则(URL 安全字符,没有前导点或下划线)。在包名中使用时,作用域前面有一个 @ 符号,后面是一个斜杠,例如

All npm packages have a name. Some package names also have a scope. A scope follows the usual rules for package names (URL-safe characters, no leading dots or underscores). When used in package names, scopes are preceded by an @ symbol and followed by a slash, e.g.


范围是一种将相关包组合在一起的方式,也影响 npm 处理包的方式的一些事情。

Scopes are a way of grouping related packages together, and also affect a few things about the way npm treats the package.

每个 npm 用户/组织都有自己的范围,只有你可以在你的范围内添加包。这意味着你不必担心有人抢先你的包名称。因此,这也是向组织触发官方包信号的好方法。

Each npm user/organization has their own scope, and only you can add packages in your scope. This means you don't have to worry about someone taking your package name ahead of you. Thus it is also a good way to signal official packages for organizations.

范围包可以从 npm@2 开始发布和安装,并且受主 npm 注册表支持。无范围包可以依赖于范围包,反之亦然。npm 客户端向后兼容无范围注册表,因此它可以同时用于作用域和无范围注册表。

Scoped packages can be published and installed as of npm@2 and are supported by the primary npm registry. Unscoped packages can depend on scoped packages and vice versa. The npm client is backwards-compatible with unscoped registries, so it can be used to work with scoped and unscoped registries at the same time.


Installing scoped packages

范围包安装到常规安装文件夹的子文件夹,例如如果你的其他包安装在 node_modules/packagename 中,则作用域模块将安装在 node_modules/@myorg/packagename 中。作用域文件夹 (@myorg) 只是作用域的名称,前面有一个 @ 符号,可以包含任意数量的范围包。

Scoped packages are installed to a sub-folder of the regular installation folder, e.g. if your other packages are installed in node_modules/packagename, scoped modules will be installed in node_modules/@myorg/packagename. The scope folder (@myorg) is simply the name of the scope preceded by an @ symbol, and can contain any number of scoped packages.

范围包是通过在 npm install 中按名称引用它来安装的,前面有一个 @ 符号:

A scoped package is installed by referencing it by name, preceded by an @ symbol, in npm install:

npm install @myorg/mypackage

或者在 package.json 中:

Or in package.json:

"dependencies": {
"@myorg/mypackage": "^1.3.0"

请注意,如果省略 @ 符号,无论哪种情况,npm 都会尝试从 GitHub 安装;见 npm install

Note that if the @ symbol is omitted, in either case, npm will instead attempt to install from GitHub; see npm install.


Requiring scoped packages


Because scoped packages are installed into a scope folder, you have to include the name of the scope when requiring them in your code, e.g.


Node 处理作用域文件夹的方式并没有什么特别之处。这仅需要名为 @myorg 的文件夹中的 mypackage 模块。

There is nothing special about the way Node treats scope folders. This simply requires the mypackage module in the folder named @myorg.


Publishing scoped packages

范围包可以从 npm@2 的 CLI 发布,并且可以发布到任何支持它们的注册表,包括主 npm 注册表。

Scoped packages can be published from the CLI as of npm@2 and can be published to any registry that supports them, including the primary npm registry.

(截至 2015 年 4 月 19 日,对于 npm 2.0 或更高版本,主 npm 注册表确实支持范围包。)

(As of 2015-04-19, and with npm 2.0 or better, the primary npm registry does support scoped packages.)


If you wish, you may associate a scope with a registry; see below.

将公共范围的包发布到主 npm 注册表

Publishing public scoped packages to the primary npm registry


Publishing to a scope, you have two options:

  • 发布到你的用户范围(例如:@username/module)

    Publishing to your user scope (example: @username/module)

  • 发布到组织范围(示例:@org/module)

    Publishing to an organization scope (example: @org/module)

如果将公共模块发布到组织范围,你必须首先使用你要发布到的范围的名称创建一个组织,或者将其添加到具有适当权限的现有组织。例如,如果你想发布到 @org,则需要在尝试发布之前在 上创建 org 组织。

If publishing a public module to an organization scope, you must first either create an organization with the name of the scope that you'd like to publish to or be added to an existing organization with the appropriate permissions. For example, if you'd like to publish to @org, you would need to create the org organization on prior to trying to publish.

默认情况下,范围包不是公共的。你需要使用初始 npm publish 命令指定 --access public。这将发布包并设置对 public 的访问权限,就像你在发布后运行 npm access public 一样。在发布现有范围包的新版本时,你不需要这样做。

Scoped packages are not public by default. You will need to specify --access public with the initial npm publish command. This will publish the package and set access to public as if you had run npm access public after publishing. You do not need to do this when publishing new versions of an existing scoped package.

将私有范围包发布到 npm 注册表

Publishing private scoped packages to the npm registry

要将私有范围包发布到 npm 注册表,你必须拥有 npm 私有模块 账户。

To publish a private scoped package to the npm registry, you must have an npm Private Modules account.

然后,你可以使用 npm publishnpm publish --access restricted 发布模块,它将出现在 npm 注册表中,但访问受限。然后,如果需要,你可以使用 npm access 或在 网站上更改访问权限。

You can then publish the module with npm publish or npm publish --access restricted, and it will be present in the npm registry, with restricted access. You can then change the access permissions, if desired, with npm access or on the website.


Associating a scope with a registry

范围可以与单独的注册表相关联。这使你可以无缝地使用来自主要 npm 注册表和一个或多个私有注册表(例如 GitHub 包 或开源 Verdaccio 项目)的包的组合。

Scopes can be associated with a separate registry. This allows you to seamlessly use a mix of packages from the primary npm registry and one or more private registries, such as GitHub Packages or the open source Verdaccio project.


You can associate a scope with a registry at login, e.g.

npm login --registry= --scope=@myco


Scopes have a many-to-one relationship with registries: one registry can host multiple scopes, but a scope only ever points to one registry.

你还可以使用 npm config 将范围与注册表关联:

You can also associate a scope with a registry using npm config:

npm config set @myco:registry

一旦范围与注册表相关联,具有该范围的包的任何 npm install 都将改为从该注册表请求包。包含范围的包名称的任何 npm publish 将改为发布到该注册表。

Once a scope is associated with a registry, any npm install for a package with that scope will request packages from that registry instead. Any npm publish for a package name that contains the scope will be published to that registry instead.


See also

npm 中文网 - 粤ICP备13048890号