Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
scope.md124 linesDownload Raw Back to using-npm
1---
2title: Scope
3section: 7
4description: Scoped packages
5---
6
7### Description
8
9All npm packages have a name.
10Some package names also have a scope.
11A scope follows the usual rules for package names (URL-safe characters, no leading dots or underscores).
12When used in package names, scopes are preceded by an `@` symbol and followed by a slash, e.g.
13
14```bash
15@somescope/somepackagename
16```
17
18Scopes are a way of grouping related packages together, and also affect a few things about the way npm treats the package.
19
20Each npm user/organization has their own scope, and only you can add packages in your scope.
21This means you don't have to worry about someone taking your package name ahead of you.
22Thus it is also a good way to signal official packages for organizations.
23
24Scoped packages can be published and installed as of `npm@2` and are supported by the primary npm registry.
25Unscoped packages can depend on scoped packages and vice versa.
26The npm client is backwards-compatible with unscoped registries, so it can be used to work with scoped and unscoped registries at the same time.
27
28### Installing scoped packages
29
30Scoped 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`.
31The scope folder (`@myorg`) is simply the name of the scope preceded by an `@` symbol, and can contain any number of scoped packages.
32
33A scoped package is installed by referencing it by name, preceded by an
34`@` symbol, in `npm install`:
35
36```bash
37npm install @myorg/mypackage
38```
39
40Or in `package.json`:
41
42```json
43"dependencies": {
44  "@myorg/mypackage": "^1.3.0"
45}
46```
47
48Note that if the `@` symbol is omitted, in either case, npm will instead attempt to install from GitHub; see [`npm install`](/commands/npm-install).
49
50### Requiring scoped packages
51
52Because 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.
53
54```javascript
55require('@myorg/mypackage')
56```
57
58There is nothing special about the way Node treats scope folders.
59This simply requires the `mypackage` module in the folder named `@myorg`.
60
61### Publishing scoped packages
62
63Scoped 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.
64
65(As of 2015-04-19, and with npm 2.0 or better, the primary npm registry
66**does** support scoped packages.)
67
68If you wish, you may associate a scope with a registry; see below.
69
70#### Publishing public scoped packages to the primary npm registry
71
72Publishing to a scope, you have two options:
73
74- Publishing to your user scope (example: `@username/module`)
75- Publishing to an organization scope (example: `@org/module`)
76
77If 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.
78For 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.
79
80Scoped packages are not public by default.
81You will need to specify
82`--access public` with the initial `npm publish` command.
83This will publish the package and set access to `public` as if you had run `npm access public` after publishing.
84You do not need to do this when publishing new versions of an existing scoped package.
85
86#### Publishing private scoped packages to the npm registry
87
88To publish a private scoped package to the npm registry, you must have an [npm Private Modules](https://docs.npmjs.com/private-modules/intro) account.
89
90You can then publish the module with `npm publish` or `npm publish
91--access restricted`, and it will be present in the npm registry, with restricted access.
92You can then change the access permissions, if desired, with `npm access` or on the npmjs.com website.
93
94### Associating a scope with a registry
95
96Scopes can be associated with a separate registry.
97This allows you to seamlessly use a mix of packages from the primary npm registry and one or more private registries, such as [GitHub Packages](https://github.com/features/packages) or the open source [Verdaccio](https://verdaccio.org) project.
98
99You can associate a scope with a registry at login, e.g.
100
101```bash
102npm login --registry=http://reg.example.com --scope=@myco
103```
104
105Scopes have a many-to-one relationship with registries: one registry can host multiple scopes, but a scope only ever points to one registry.
106
107You can also associate a scope with a registry using `npm config`:
108
109```bash
110npm config set @myco:registry=http://reg.example.com
111```
112
113Once a scope is associated with a registry, any `npm install` for a package with that scope will request packages from that registry instead.
114Any
115`npm publish` for a package name that contains the scope will be published to
116that registry instead.
117
118### See also
119
120* [npm install](/commands/npm-install)
121* [npm publish](/commands/npm-publish)
122* [npm access](/commands/npm-access)
123* [npm registry](/using-npm/registry)
124 
codekingpro/portable-devtools · Team Ai