11cb0ef41Sopenharmony_ci---
21cb0ef41Sopenharmony_cititle: scope
31cb0ef41Sopenharmony_cisection: 7
41cb0ef41Sopenharmony_cidescription: Scoped packages
51cb0ef41Sopenharmony_ci---
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci### Description
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciAll npm packages have a name. Some package names also have a scope. A scope
101cb0ef41Sopenharmony_cifollows the usual rules for package names (URL-safe characters, no leading dots
111cb0ef41Sopenharmony_cior underscores). When used in package names, scopes are preceded by an `@` symbol
121cb0ef41Sopenharmony_ciand followed by a slash, e.g.
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci```bash
151cb0ef41Sopenharmony_ci@somescope/somepackagename
161cb0ef41Sopenharmony_ci```
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciScopes are a way of grouping related packages together, and also affect a few
191cb0ef41Sopenharmony_cithings about the way npm treats the package.
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ciEach npm user/organization has their own scope, and only you can add packages
221cb0ef41Sopenharmony_ciin your scope. This means you don't have to worry about someone taking your
231cb0ef41Sopenharmony_cipackage name ahead of you. Thus it is also a good way to signal official packages
241cb0ef41Sopenharmony_cifor organizations.
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ciScoped packages can be published and installed as of `npm@2` and are supported
271cb0ef41Sopenharmony_ciby the primary npm registry. Unscoped packages can depend on scoped packages and
281cb0ef41Sopenharmony_civice versa. The npm client is backwards-compatible with unscoped registries,
291cb0ef41Sopenharmony_ciso it can be used to work with scoped and unscoped registries at the same time.
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci### Installing scoped packages
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ciScoped packages are installed to a sub-folder of the regular installation
341cb0ef41Sopenharmony_cifolder, e.g. if your other packages are installed in `node_modules/packagename`,
351cb0ef41Sopenharmony_ciscoped modules will be installed in `node_modules/@myorg/packagename`. The scope
361cb0ef41Sopenharmony_cifolder (`@myorg`) is simply the name of the scope preceded by an `@` symbol, and can
371cb0ef41Sopenharmony_cicontain any number of scoped packages.
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ciA scoped package is installed by referencing it by name, preceded by an
401cb0ef41Sopenharmony_ci`@` symbol, in `npm install`:
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci```bash
431cb0ef41Sopenharmony_cinpm install @myorg/mypackage
441cb0ef41Sopenharmony_ci```
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ciOr in `package.json`:
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci```json
491cb0ef41Sopenharmony_ci"dependencies": {
501cb0ef41Sopenharmony_ci  "@myorg/mypackage": "^1.3.0"
511cb0ef41Sopenharmony_ci}
521cb0ef41Sopenharmony_ci```
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ciNote that if the `@` symbol is omitted, in either case, npm will instead attempt to
551cb0ef41Sopenharmony_ciinstall from GitHub; see [`npm install`](/commands/npm-install).
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci### Requiring scoped packages
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ciBecause scoped packages are installed into a scope folder, you have to
601cb0ef41Sopenharmony_ciinclude the name of the scope when requiring them in your code, e.g.
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci```javascript
631cb0ef41Sopenharmony_cirequire('@myorg/mypackage')
641cb0ef41Sopenharmony_ci```
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ciThere is nothing special about the way Node treats scope folders. This
671cb0ef41Sopenharmony_cisimply requires the `mypackage` module in the folder named `@myorg`.
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci### Publishing scoped packages
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ciScoped packages can be published from the CLI as of `npm@2` and can be
721cb0ef41Sopenharmony_cipublished to any registry that supports them, including the primary npm
731cb0ef41Sopenharmony_ciregistry.
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci(As of 2015-04-19, and with npm 2.0 or better, the primary npm registry
761cb0ef41Sopenharmony_ci**does** support scoped packages.)
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ciIf you wish, you may associate a scope with a registry; see below.
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci#### Publishing public scoped packages to the primary npm registry
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ciPublishing to a scope, you have two options:
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci- Publishing to your user scope (example: `@username/module`)
851cb0ef41Sopenharmony_ci- Publishing to an organization scope (example: `@org/module`)
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ciIf publishing a public module to an organization scope, you must
881cb0ef41Sopenharmony_cifirst either create an organization with the name of the scope
891cb0ef41Sopenharmony_cithat you'd like to publish to or be added to an existing organization
901cb0ef41Sopenharmony_ciwith the appropriate permissions. For example, if you'd like to
911cb0ef41Sopenharmony_cipublish to `@org`, you would  need to create the `org` organization
921cb0ef41Sopenharmony_cion npmjs.com prior to trying to publish.
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_ciScoped packages are not public by default.  You will need to specify
951cb0ef41Sopenharmony_ci`--access public` with the initial `npm publish` command.  This will publish
961cb0ef41Sopenharmony_cithe package and set access to `public` as if you had run `npm access public`
971cb0ef41Sopenharmony_ciafter publishing.  You do not need to do this when publishing new versions of
981cb0ef41Sopenharmony_cian existing scoped package.
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ci#### Publishing private scoped packages to the npm registry
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ciTo publish a private scoped package to the npm registry, you must have
1031cb0ef41Sopenharmony_cian [npm Private Modules](https://docs.npmjs.com/private-modules/intro)
1041cb0ef41Sopenharmony_ciaccount.
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_ciYou can then publish the module with `npm publish` or `npm publish
1071cb0ef41Sopenharmony_ci--access restricted`, and it will be present in the npm registry, with
1081cb0ef41Sopenharmony_cirestricted access. You can then change the access permissions, if
1091cb0ef41Sopenharmony_cidesired, with `npm access` or on the npmjs.com website.
1101cb0ef41Sopenharmony_ci
1111cb0ef41Sopenharmony_ci### Associating a scope with a registry
1121cb0ef41Sopenharmony_ci
1131cb0ef41Sopenharmony_ciScopes can be associated with a separate registry. This allows you to
1141cb0ef41Sopenharmony_ciseamlessly use a mix of packages from the primary npm registry and one or more
1151cb0ef41Sopenharmony_ciprivate registries, such as [GitHub Packages](https://github.com/features/packages) or the open source [Verdaccio](https://verdaccio.org)
1161cb0ef41Sopenharmony_ciproject.
1171cb0ef41Sopenharmony_ci
1181cb0ef41Sopenharmony_ciYou can associate a scope with a registry at login, e.g.
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ci```bash
1211cb0ef41Sopenharmony_cinpm login --registry=http://reg.example.com --scope=@myco
1221cb0ef41Sopenharmony_ci```
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_ciScopes have a many-to-one relationship with registries: one registry can
1251cb0ef41Sopenharmony_cihost multiple scopes, but a scope only ever points to one registry.
1261cb0ef41Sopenharmony_ci
1271cb0ef41Sopenharmony_ciYou can also associate a scope with a registry using `npm config`:
1281cb0ef41Sopenharmony_ci
1291cb0ef41Sopenharmony_ci```bash
1301cb0ef41Sopenharmony_cinpm config set @myco:registry=http://reg.example.com
1311cb0ef41Sopenharmony_ci```
1321cb0ef41Sopenharmony_ci
1331cb0ef41Sopenharmony_ciOnce a scope is associated with a registry, any `npm install` for a package
1341cb0ef41Sopenharmony_ciwith that scope will request packages from that registry instead. Any
1351cb0ef41Sopenharmony_ci`npm publish` for a package name that contains the scope will be published to
1361cb0ef41Sopenharmony_cithat registry instead.
1371cb0ef41Sopenharmony_ci
1381cb0ef41Sopenharmony_ci### See also
1391cb0ef41Sopenharmony_ci
1401cb0ef41Sopenharmony_ci* [npm install](/commands/npm-install)
1411cb0ef41Sopenharmony_ci* [npm publish](/commands/npm-publish)
1421cb0ef41Sopenharmony_ci* [npm access](/commands/npm-access)
1431cb0ef41Sopenharmony_ci* [npm registry](/using-npm/registry)
144