npm-pkg.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <html><head>
  2. <title>npm-pkg</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="npm-pkg">npm-pkg</h1>
  125. <span class="description">Manages your package.json</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="#see-also">See Also</a></li></ul></div>
  130. </section>
  131. <div id="_content"><h3 id="synopsis">Synopsis</h3>
  132. <pre lang="bash"><code>npm pkg get [&lt;field&gt; [.&lt;subfield&gt; ...]]
  133. npm pkg set &lt;field&gt;=&lt;value&gt; [.&lt;subfield&gt;=&lt;value&gt; ...]
  134. npm pkg delete &lt;field&gt; [.&lt;subfield&gt; ...]
  135. </code></pre>
  136. <h3 id="description">Description</h3>
  137. <p>A command that automates the management of <code>package.json</code> files.
  138. <code>npm pkg</code> provide 3 different sub commands that allow you to modify or retrieve
  139. values for given object keys in your <code>package.json</code>.</p>
  140. <p>The syntax to retrieve and set fields is a dot separated representation of
  141. the nested object properties to be found within your <code>package.json</code>, it’s the
  142. same notation used in <a href="../commands/npm-view.html"><code>npm view</code></a> to retrieve information
  143. from the registry manifest, below you can find more examples on how to use it.</p>
  144. <p>Returned values are always in <strong>json</strong> format.</p>
  145. <ul>
  146. <li>
  147. <p><code>npm pkg get &lt;field&gt;</code></p>
  148. <p>Retrieves a value <code>key</code>, defined in your <code>package.json</code> file.</p>
  149. <p>For example, in order to retrieve the name of the current package, you
  150. can run:</p>
  151. <pre lang="bash"><code>npm pkg get name
  152. </code></pre>
  153. <p>It’s also possible to retrieve multiple values at once:</p>
  154. <pre lang="bash"><code>npm pkg get name version
  155. </code></pre>
  156. <p>You can view child fields by separating them with a period. To retrieve
  157. the value of a test <code>script</code> value, you would run the following command:</p>
  158. <pre lang="bash"><code>npm pkg get scripts.test
  159. </code></pre>
  160. <p>For fields that are arrays, requesting a non-numeric field will return
  161. all of the values from the objects in the list. For example, to get all
  162. the contributor emails for a package, you would run:</p>
  163. <pre lang="bash"><code>npm pkg get contributors.email
  164. </code></pre>
  165. <p>You may also use numeric indices in square braces to specifically select
  166. an item in an array field. To just get the email address of the first
  167. contributor in the list, you can run:</p>
  168. <pre lang="bash"><code>npm pkg get contributors[0].email
  169. </code></pre>
  170. </li>
  171. <li>
  172. <p><code>npm pkg set &lt;field&gt;=&lt;value&gt;</code></p>
  173. <p>Sets a <code>value</code> in your <code>package.json</code> based on the <code>field</code> value. When
  174. saving to your <code>package.json</code> file the same set of rules used during
  175. <code>npm install</code> and other cli commands that touches the <code>package.json</code> file
  176. are used, making sure to respect the existing indentation and possibly
  177. applying some validation prior to saving values to the file.</p>
  178. <p>The same syntax used to retrieve values from your package can also be used
  179. to define new properties or overriding existing ones, below are some
  180. examples of how the dot separated syntax can be used to edit your
  181. <code>package.json</code> file.</p>
  182. <p>Defining a new bin named <code>mynewcommand</code> in your <code>package.json</code> that points
  183. to a file <code>cli.js</code>:</p>
  184. <pre lang="bash"><code>npm pkg set bin.mynewcommand=cli.js
  185. </code></pre>
  186. <p>Setting multiple fields at once is also possible:</p>
  187. <pre lang="bash"><code>npm pkg set description='Awesome package' engines.node='&gt;=10'
  188. </code></pre>
  189. <p>It’s also possible to add to array values, for example to add a new
  190. contributor entry:</p>
  191. <pre lang="bash"><code>npm pkg set contributors[0].name='Foo' contributors[0].email='foo@bar.ca'
  192. </code></pre>
  193. <p>You may also append items to the end of an array using the special
  194. empty bracket notation:</p>
  195. <pre lang="bash"><code>npm pkg set contributors[].name='Foo' contributors[].name='Bar'
  196. </code></pre>
  197. <p>It’s also possible to parse values as json prior to saving them to your
  198. <code>package.json</code> file, for example in order to set a <code>"private": true</code>
  199. property:</p>
  200. <pre lang="bash"><code>npm pkg set private=true --json
  201. </code></pre>
  202. <p>It also enables saving values as numbers:</p>
  203. <pre lang="bash"><code>npm pkg set tap.timeout=60 --json
  204. </code></pre>
  205. </li>
  206. <li>
  207. <p><code>npm pkg delete &lt;key&gt;</code></p>
  208. <p>Deletes a <code>key</code> from your <code>package.json</code></p>
  209. <p>The same syntax used to set values from your package can also be used
  210. to remove existing ones. For example, in order to remove a script named
  211. build:</p>
  212. <pre lang="bash"><code>npm pkg delete scripts.build
  213. </code></pre>
  214. </li>
  215. </ul>
  216. <h3 id="workspaces-support">Workspaces support</h3>
  217. <p>You can set/get/delete items across your configured workspaces by using the
  218. <code>workspace</code> or <code>workspaces</code> config options.</p>
  219. <p>For example, setting a <code>funding</code> value across all configured workspaces
  220. of a project:</p>
  221. <pre lang="bash"><code>npm pkg set funding=https://example.com --ws
  222. </code></pre>
  223. <p>When using <code>npm pkg get</code> to retrieve info from your configured workspaces, the
  224. returned result will be in a json format in which top level keys are the
  225. names of each workspace, the values of these keys will be the result values
  226. returned from each of the configured workspaces, e.g:</p>
  227. <pre><code>npm pkg get name version --ws
  228. {
  229. "a": {
  230. "name": "a",
  231. "version": "1.0.0"
  232. },
  233. "b": {
  234. "name": "b",
  235. "version": "1.0.0"
  236. }
  237. }
  238. </code></pre>
  239. <h3 id="configuration">Configuration</h3>
  240. <!-- raw HTML omitted -->
  241. <!-- raw HTML omitted -->
  242. <!-- raw HTML omitted -->
  243. <h4 id="force"><code>force</code></h4>
  244. <ul>
  245. <li>Default: false</li>
  246. <li>Type: Boolean</li>
  247. </ul>
  248. <p>Removes various protections against unfortunate side effects, common
  249. mistakes, unnecessary performance degradation, and malicious input.</p>
  250. <ul>
  251. <li>Allow clobbering non-npm files in global installs.</li>
  252. <li>Allow the <code>npm version</code> command to work on an unclean git repository.</li>
  253. <li>Allow deleting the cache folder with <code>npm cache clean</code>.</li>
  254. <li>Allow installing packages that have an <code>engines</code> declaration requiring a
  255. different version of npm.</li>
  256. <li>Allow installing packages that have an <code>engines</code> declaration requiring a
  257. different version of <code>node</code>, even if <code>--engine-strict</code> is enabled.</li>
  258. <li>Allow <code>npm audit fix</code> to install modules outside your stated dependency
  259. range (including SemVer-major changes).</li>
  260. <li>Allow unpublishing all versions of a published package.</li>
  261. <li>Allow conflicting peerDependencies to be installed in the root project.</li>
  262. <li>Implicitly set <code>--yes</code> during <code>npm init</code>.</li>
  263. <li>Allow clobbering existing values in <code>npm pkg</code></li>
  264. </ul>
  265. <p>If you don’t have a clear idea of what you want to do, it is strongly
  266. recommended that you do not use this option!</p>
  267. <!-- raw HTML omitted -->
  268. <!-- raw HTML omitted -->
  269. <h4 id="json"><code>json</code></h4>
  270. <ul>
  271. <li>Default: false</li>
  272. <li>Type: Boolean</li>
  273. </ul>
  274. <p>Whether or not to output JSON data, rather than the normal output.</p>
  275. <ul>
  276. <li>In <code>npm pkg set</code> it enables parsing set values with JSON.parse() before
  277. saving them to your <code>package.json</code>.</li>
  278. </ul>
  279. <p>Not supported by all npm commands.</p>
  280. <!-- raw HTML omitted -->
  281. <!-- raw HTML omitted -->
  282. <h4 id="workspace"><code>workspace</code></h4>
  283. <ul>
  284. <li>Default:</li>
  285. <li>Type: String (can be set multiple times)</li>
  286. </ul>
  287. <p>Enable running a command in the context of the configured workspaces of the
  288. current project while filtering by running only the workspaces defined by
  289. this configuration option.</p>
  290. <p>Valid values for the <code>workspace</code> config are either:</p>
  291. <ul>
  292. <li>Workspace names</li>
  293. <li>Path to a workspace directory</li>
  294. <li>Path to a parent workspace directory (will result in selecting all
  295. workspaces within that folder)</li>
  296. </ul>
  297. <p>When set for the <code>npm init</code> command, this may be set to the folder of a
  298. workspace which does not yet exist, to create the folder and set it up as a
  299. brand new workspace within the project.</p>
  300. <p>This value is not exported to the environment for child processes.</p>
  301. <!-- raw HTML omitted -->
  302. <!-- raw HTML omitted -->
  303. <h4 id="workspaces"><code>workspaces</code></h4>
  304. <ul>
  305. <li>Default: null</li>
  306. <li>Type: null or Boolean</li>
  307. </ul>
  308. <p>Set to true to run the command in the context of <strong>all</strong> configured
  309. workspaces.</p>
  310. <p>Explicitly setting this to false will cause commands like <code>install</code> to
  311. ignore workspaces altogether. When not set explicitly:</p>
  312. <ul>
  313. <li>Commands that operate on the <code>node_modules</code> tree (install, update, etc.)
  314. will link workspaces into the <code>node_modules</code> folder. - Commands that do
  315. other things (test, exec, publish, etc.) will operate on the root project,
  316. <em>unless</em> one or more workspaces are specified in the <code>workspace</code> config.</li>
  317. </ul>
  318. <p>This value is not exported to the environment for child processes.</p>
  319. <!-- raw HTML omitted -->
  320. <!-- raw HTML omitted -->
  321. <!-- raw HTML omitted -->
  322. <h2 id="see-also">See Also</h2>
  323. <ul>
  324. <li><a href="../commands/npm-install.html">npm install</a></li>
  325. <li><a href="../commands/npm-init.html">npm init</a></li>
  326. <li><a href="../commands/npm-config.html">npm config</a></li>
  327. <li><a href="../commands/npm-set-script.html">npm set-script</a></li>
  328. <li><a href="../using-npm/workspaces.html">workspaces</a></li>
  329. </ul>
  330. </div>
  331. <footer id="edit">
  332. <a href="https://github.com/npm/cli/edit/latest/docs/content/commands/npm-pkg.md">
  333. <svg role="img" viewBox="0 0 16 16" width="16" height="16" fill="currentcolor" style="vertical-align: text-bottom; margin-right: 0.3em;">
  334. <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>
  335. </svg>
  336. Edit this page on GitHub
  337. </a>
  338. </footer>
  339. </section>
  340. </body></html>