scope.html 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <html><head>
  2. <title>scope</title>
  3. <style>
  4. body {
  5. background-color: #ffffff;
  6. color: #24292e;
  7. margin: 0;
  8. line-height: 1.5;
  9. font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji";
  10. }
  11. #rainbar {
  12. height: 10px;
  13. background-image: linear-gradient(139deg, #fb8817, #ff4b01, #c12127, #e02aff);
  14. }
  15. a {
  16. text-decoration: none;
  17. color: #0366d6;
  18. }
  19. a:hover {
  20. text-decoration: underline;
  21. }
  22. pre {
  23. margin: 1em 0px;
  24. padding: 1em;
  25. border: solid 1px #e1e4e8;
  26. border-radius: 6px;
  27. display: block;
  28. overflow: auto;
  29. white-space: pre;
  30. background-color: #f6f8fa;
  31. color: #393a34;
  32. }
  33. code {
  34. font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace;
  35. font-size: 85%;
  36. padding: 0.2em 0.4em;
  37. background-color: #f6f8fa;
  38. color: #393a34;
  39. }
  40. pre > code {
  41. padding: 0;
  42. background-color: inherit;
  43. color: inherit;
  44. }
  45. h1, h2, h3 {
  46. font-weight: 600;
  47. }
  48. #logobar {
  49. background-color: #333333;
  50. margin: 0 auto;
  51. padding: 1em 4em;
  52. }
  53. #logobar .logo {
  54. float: left;
  55. }
  56. #logobar .title {
  57. font-weight: 600;
  58. color: #dddddd;
  59. float: left;
  60. margin: 5px 0 0 1em;
  61. }
  62. #logobar:after {
  63. content: "";
  64. display: block;
  65. clear: both;
  66. }
  67. #content {
  68. margin: 0 auto;
  69. padding: 0 4em;
  70. }
  71. #table_of_contents > h2 {
  72. font-size: 1.17em;
  73. }
  74. #table_of_contents ul:first-child {
  75. border: solid 1px #e1e4e8;
  76. border-radius: 6px;
  77. padding: 1em;
  78. background-color: #f6f8fa;
  79. color: #393a34;
  80. }
  81. #table_of_contents ul {
  82. list-style-type: none;
  83. padding-left: 1.5em;
  84. }
  85. #table_of_contents li {
  86. font-size: 0.9em;
  87. }
  88. #table_of_contents li a {
  89. color: #000000;
  90. }
  91. header.title {
  92. border-bottom: solid 1px #e1e4e8;
  93. }
  94. header.title > h1 {
  95. margin-bottom: 0.25em;
  96. }
  97. header.title > .description {
  98. display: block;
  99. margin-bottom: 0.5em;
  100. line-height: 1;
  101. }
  102. footer#edit {
  103. border-top: solid 1px #e1e4e8;
  104. margin: 3em 0 4em 0;
  105. padding-top: 2em;
  106. }
  107. </style>
  108. </head>
  109. <body>
  110. <div id="banner">
  111. <div id="rainbar"></div>
  112. <div id="logobar">
  113. <svg class="logo" role="img" height="32" width="32" viewBox="0 0 700 700">
  114. <polygon fill="#cb0000" points="0,700 700,700 700,0 0,0"></polygon>
  115. <polygon fill="#ffffff" points="150,550 350,550 350,250 450,250 450,550 550,550 550,150 150,150"></polygon>
  116. </svg>
  117. <div class="title">
  118. npm command-line interface
  119. </div>
  120. </div>
  121. </div>
  122. <section id="content">
  123. <header class="title">
  124. <h1 id="scope">scope</h1>
  125. <span class="description">Scoped packages</span>
  126. </header>
  127. <section id="table_of_contents">
  128. <h2 id="table-of-contents">Table of contents</h2>
  129. <div id="_table_of_contents"><ul><li><a href="#description">Description</a></li><li><a href="#installing-scoped-packages">Installing scoped packages</a></li><li><a href="#requiring-scoped-packages">Requiring scoped packages</a></li><li><a href="#publishing-scoped-packages">Publishing scoped packages</a></li><ul><li><a href="#publishing-public-scoped-packages-to-the-primary-npm-registry">Publishing public scoped packages to the primary npm registry</a></li><li><a href="#publishing-private-scoped-packages-to-the-npm-registry">Publishing private scoped packages to the npm registry</a></li></ul><li><a href="#associating-a-scope-with-a-registry">Associating a scope with a registry</a></li><li><a href="#see-also">See also</a></li></ul></div>
  130. </section>
  131. <div id="_content"><h3 id="description">Description</h3>
  132. <p>All npm packages have a name. Some package names also have a scope. A scope
  133. follows the usual rules for package names (URL-safe characters, no leading dots
  134. or underscores). When used in package names, scopes are preceded by an <code>@</code> symbol
  135. and followed by a slash, e.g.</p>
  136. <pre lang="bash"><code>@somescope/somepackagename
  137. </code></pre>
  138. <p>Scopes are a way of grouping related packages together, and also affect a few
  139. things about the way npm treats the package.</p>
  140. <p>Each npm user/organization has their own scope, and only you can add packages
  141. in your scope. This means you don’t have to worry about someone taking your
  142. package name ahead of you. Thus it is also a good way to signal official packages
  143. for organizations.</p>
  144. <p>Scoped packages can be published and installed as of <code>npm@2</code> and are supported
  145. by the primary npm registry. Unscoped packages can depend on scoped packages and
  146. vice versa. The npm client is backwards-compatible with unscoped registries,
  147. so it can be used to work with scoped and unscoped registries at the same time.</p>
  148. <h3 id="installing-scoped-packages">Installing scoped packages</h3>
  149. <p>Scoped packages are installed to a sub-folder of the regular installation
  150. folder, e.g. if your other packages are installed in <code>node_modules/packagename</code>,
  151. scoped modules will be installed in <code>node_modules/@myorg/packagename</code>. The scope
  152. folder (<code>@myorg</code>) is simply the name of the scope preceded by an <code>@</code> symbol, and can
  153. contain any number of scoped packages.</p>
  154. <p>A scoped package is installed by referencing it by name, preceded by an
  155. <code>@</code> symbol, in <code>npm install</code>:</p>
  156. <pre lang="bash"><code>npm install @myorg/mypackage
  157. </code></pre>
  158. <p>Or in <code>package.json</code>:</p>
  159. <pre lang="json"><code>"dependencies": {
  160. "@myorg/mypackage": "^1.3.0"
  161. }
  162. </code></pre>
  163. <p>Note that if the <code>@</code> symbol is omitted, in either case, npm will instead attempt to
  164. install from GitHub; see <a href="../commands/npm-install.html"><code>npm install</code></a>.</p>
  165. <h3 id="requiring-scoped-packages">Requiring scoped packages</h3>
  166. <p>Because scoped packages are installed into a scope folder, you have to
  167. include the name of the scope when requiring them in your code, e.g.</p>
  168. <pre lang="javascript"><code>require('@myorg/mypackage')
  169. </code></pre>
  170. <p>There is nothing special about the way Node treats scope folders. This
  171. simply requires the <code>mypackage</code> module in the folder named <code>@myorg</code>.</p>
  172. <h3 id="publishing-scoped-packages">Publishing scoped packages</h3>
  173. <p>Scoped packages can be published from the CLI as of <code>npm@2</code> and can be
  174. published to any registry that supports them, including the primary npm
  175. registry.</p>
  176. <p>(As of 2015-04-19, and with npm 2.0 or better, the primary npm registry
  177. <strong>does</strong> support scoped packages.)</p>
  178. <p>If you wish, you may associate a scope with a registry; see below.</p>
  179. <h4 id="publishing-public-scoped-packages-to-the-primary-npm-registry">Publishing public scoped packages to the primary npm registry</h4>
  180. <p>To publish a public scoped package, you must specify <code>--access public</code> with
  181. the initial publication. This will publish the package and set access
  182. to <code>public</code> as if you had run <code>npm access public</code> after publishing.</p>
  183. <h4 id="publishing-private-scoped-packages-to-the-npm-registry">Publishing private scoped packages to the npm registry</h4>
  184. <p>To publish a private scoped package to the npm registry, you must have
  185. an <a href="https://docs.npmjs.com/private-modules/intro">npm Private Modules</a>
  186. account.</p>
  187. <p>You can then publish the module with <code>npm publish</code> or <code>npm publish --access restricted</code>, and it will be present in the npm registry, with
  188. restricted access. You can then change the access permissions, if
  189. desired, with <code>npm access</code> or on the npmjs.com website.</p>
  190. <h3 id="associating-a-scope-with-a-registry">Associating a scope with a registry</h3>
  191. <p>Scopes can be associated with a separate registry. This allows you to
  192. seamlessly use a mix of packages from the primary npm registry and one or more
  193. private registries, such as <a href="https://github.com/features/packages">GitHub Packages</a> or the open source <a href="https://verdaccio.org">Verdaccio</a>
  194. project.</p>
  195. <p>You can associate a scope with a registry at login, e.g.</p>
  196. <pre lang="bash"><code>npm login --registry=http://reg.example.com --scope=@myco
  197. </code></pre>
  198. <p>Scopes have a many-to-one relationship with registries: one registry can
  199. host multiple scopes, but a scope only ever points to one registry.</p>
  200. <p>You can also associate a scope with a registry using <code>npm config</code>:</p>
  201. <pre lang="bash"><code>npm config set @myco:registry http://reg.example.com
  202. </code></pre>
  203. <p>Once a scope is associated with a registry, any <code>npm install</code> for a package
  204. with that scope will request packages from that registry instead. Any
  205. <code>npm publish</code> for a package name that contains the scope will be published to
  206. that registry instead.</p>
  207. <h3 id="see-also">See also</h3>
  208. <ul>
  209. <li><a href="../commands/npm-install.html">npm install</a></li>
  210. <li><a href="../commands/npm-publish.html">npm publish</a></li>
  211. <li><a href="../commands/npm-access.html">npm access</a></li>
  212. <li><a href="../using-npm/registry.html">npm registry</a></li>
  213. </ul>
  214. </div>
  215. <footer id="edit">
  216. <a href="https://github.com/npm/cli/edit/latest/docs/content/using-npm/scope.md">
  217. <svg role="img" viewBox="0 0 16 16" width="16" height="16" fill="currentcolor" style="vertical-align: text-bottom; margin-right: 0.3em;">
  218. <path fill-rule="evenodd" d="M11.013 1.427a1.75 1.75 0 012.474 0l1.086 1.086a1.75 1.75 0 010 2.474l-8.61 8.61c-.21.21-.47.364-.756.445l-3.251.93a.75.75 0 01-.927-.928l.929-3.25a1.75 1.75 0 01.445-.758l8.61-8.61zm1.414 1.06a.25.25 0 00-.354 0L10.811 3.75l1.439 1.44 1.263-1.263a.25.25 0 000-.354l-1.086-1.086zM11.189 6.25L9.75 4.81l-6.286 6.287a.25.25 0 00-.064.108l-.558 1.953 1.953-.558a.249.249 0 00.108-.064l6.286-6.286z"></path>
  219. </svg>
  220. Edit this page on GitHub
  221. </a>
  222. </footer>
  223. </section>
  224. </body></html>