选择命令行版本:
🌐 Description
所有 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.
@somescope/somepackagename
范围是一种将相关包组合在一起的方式,也影响 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.
require("@myorg/mypackage");
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注册表确实支持作用域包。)
如果你愿意,可以将范围与注册表关联;见下文。
🌐 If you wish, you may associate a scope with a registry; see below.
🌐 Publishing public scoped packages to the primary npm registry
发布到范围,你有两种选择:
🌐 Publishing to a scope, you have two options:
@username/module)@org/module)如果要将公共模块发布到组织范围,你必须首先创建一个使用你希望发布的范围名称的组织,或被添加到具有适当权限的现有组织中。例如,如果你想发布到 @org,则需要在尝试发布之前先在 npmjs.com 上创建 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 npmjs.com 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.
🌐 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 publish 或 npm publish --access restricted 发布该模块,它将出现在 npm 注册表中,并且访问受限。如果需要,你可以使用 npm access 或在 npmjs.com 网站上更改访问权限。
🌐 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 npmjs.com website.
🌐 Associating a scope with a registry
作用域可以与单独的注册表关联。这允许你无缝地混合使用来自主要 npm 注册表和一个或多个私有注册表的包,例如 GitHub Packages 或开源的 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=http://reg.example.com --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 http://reg.example.com
一旦范围与注册表关联,任何带有该范围的包的 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