npm-query.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. <!DOCTYPE html><html><head>
  2. <meta charset="utf-8">
  3. <title>npm-query</title>
  4. <style>
  5. body {
  6. background-color: #ffffff;
  7. color: #24292e;
  8. margin: 0;
  9. line-height: 1.5;
  10. font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji";
  11. }
  12. #rainbar {
  13. height: 10px;
  14. background-image: linear-gradient(139deg, #fb8817, #ff4b01, #c12127, #e02aff);
  15. }
  16. a {
  17. text-decoration: none;
  18. color: #0366d6;
  19. }
  20. a:hover {
  21. text-decoration: underline;
  22. }
  23. pre {
  24. margin: 1em 0px;
  25. padding: 1em;
  26. border: solid 1px #e1e4e8;
  27. border-radius: 6px;
  28. display: block;
  29. overflow: auto;
  30. white-space: pre;
  31. background-color: #f6f8fa;
  32. color: #393a34;
  33. }
  34. code {
  35. font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace;
  36. font-size: 85%;
  37. padding: 0.2em 0.4em;
  38. background-color: #f6f8fa;
  39. color: #393a34;
  40. }
  41. pre > code {
  42. padding: 0;
  43. background-color: inherit;
  44. color: inherit;
  45. }
  46. h1, h2, h3 {
  47. font-weight: 600;
  48. }
  49. #logobar {
  50. background-color: #333333;
  51. margin: 0 auto;
  52. padding: 1em 4em;
  53. }
  54. #logobar .logo {
  55. float: left;
  56. }
  57. #logobar .title {
  58. font-weight: 600;
  59. color: #dddddd;
  60. float: left;
  61. margin: 5px 0 0 1em;
  62. }
  63. #logobar:after {
  64. content: "";
  65. display: block;
  66. clear: both;
  67. }
  68. #content {
  69. margin: 0 auto;
  70. padding: 0 4em;
  71. }
  72. #table_of_contents > h2 {
  73. font-size: 1.17em;
  74. }
  75. #table_of_contents ul:first-child {
  76. border: solid 1px #e1e4e8;
  77. border-radius: 6px;
  78. padding: 1em;
  79. background-color: #f6f8fa;
  80. color: #393a34;
  81. }
  82. #table_of_contents ul {
  83. list-style-type: none;
  84. padding-left: 1.5em;
  85. }
  86. #table_of_contents li {
  87. font-size: 0.9em;
  88. }
  89. #table_of_contents li a {
  90. color: #000000;
  91. }
  92. header.title {
  93. border-bottom: solid 1px #e1e4e8;
  94. }
  95. header.title > h1 {
  96. margin-bottom: 0.25em;
  97. }
  98. header.title > .description {
  99. display: block;
  100. margin-bottom: 0.5em;
  101. line-height: 1;
  102. }
  103. header.title .version {
  104. font-size: 0.8em;
  105. color: #666666;
  106. }
  107. footer#edit {
  108. border-top: solid 1px #e1e4e8;
  109. margin: 3em 0 4em 0;
  110. padding-top: 2em;
  111. }
  112. table {
  113. width: 100%;
  114. margin: 1em 0;
  115. border-radius: 6px;
  116. border: 1px solid #e1e4e8;
  117. overflow: hidden;
  118. border-collapse: separate;
  119. border-spacing: 0;
  120. }
  121. table thead {
  122. background-color: #f6f8fa;
  123. }
  124. table tbody {
  125. background-color: #ffffff;
  126. }
  127. table th,
  128. table td {
  129. padding: 0.75em;
  130. text-align: left;
  131. border-right: 1px solid #e1e4e8;
  132. border-bottom: 1px solid #e1e4e8;
  133. }
  134. table th:last-child,
  135. table td:last-child {
  136. border-right: none;
  137. }
  138. table tbody tr:last-child td {
  139. border-bottom: none;
  140. }
  141. table th {
  142. font-weight: 600;
  143. background-color: #f6f8fa;
  144. }
  145. table code {
  146. white-space: nowrap;
  147. }
  148. </style>
  149. </head>
  150. <body>
  151. <div id="banner">
  152. <div id="rainbar"></div>
  153. <div id="logobar">
  154. <svg class="logo" role="img" height="32" width="32" viewBox="0 0 700 700">
  155. <polygon fill="#cb0000" points="0,700 700,700 700,0 0,0"></polygon>
  156. <polygon fill="#ffffff" points="150,550 350,550 350,250 450,250 450,550 550,550 550,150 150,150"></polygon>
  157. </svg>
  158. <div class="title">
  159. npm command-line interface
  160. </div>
  161. </div>
  162. </div>
  163. <section id="content">
  164. <header class="title">
  165. <h1 id="----npm-query----11120">
  166. <span>npm-query</span>
  167. <span class="version">@11.12.0</span>
  168. </h1>
  169. <span class="description">Dependency selector query</span>
  170. </header>
  171. <section id="table_of_contents">
  172. <h2 id="table-of-contents">Table of contents</h2>
  173. <div id="_table_of_contents"><ul><li><a href="#see-also">See Also</a></li></ul></div>
  174. </section>
  175. <div id="_content"><h3 id="synopsis">Synopsis</h3>
  176. <pre><code class="language-bash">npm query &lt;selector&gt;
  177. </code></pre>
  178. <h3 id="description">Description</h3>
  179. <p>The <code>npm query</code> command allows for usage of css selectors in order to retrieve an array of dependency objects.</p>
  180. <h3 id="piping-npm-query-to-other-commands">Piping npm query to other commands</h3>
  181. <pre><code class="language-bash"># find all dependencies with postinstall scripts &amp; uninstall them
  182. npm query ":attr(scripts, [postinstall])" | jq 'map(.name)|join("\n")' -r | xargs -I {} npm uninstall {}
  183. # find all git dependencies &amp; explain who requires them
  184. npm query ":type(git)" | jq 'map(.name)' | xargs -I {} npm why {}
  185. </code></pre>
  186. <h3 id="extended-use-cases--queries">Extended Use Cases &amp; Queries</h3>
  187. <pre><code class="language-stylus">// all deps
  188. *
  189. // all direct deps
  190. :root &gt; *
  191. // direct production deps
  192. :root &gt; .prod
  193. // direct development deps
  194. :root &gt; .dev
  195. // any peer dep of a direct deps
  196. :root &gt; * &gt; .peer
  197. // any workspace dep
  198. .workspace
  199. // all workspaces that depend on another workspace
  200. .workspace &gt; .workspace
  201. // all workspaces that have peer deps
  202. .workspace:has(.peer)
  203. // any dep named "lodash"
  204. // equivalent to [name="lodash"]
  205. #lodash
  206. // any deps named "lodash" &amp; within semver range ^"1.2.3"
  207. #lodash@^1.2.3
  208. // equivalent to...
  209. [name="lodash"]:semver(^1.2.3)
  210. // get the hoisted node for a given semver range
  211. #lodash@^1.2.3:not(:deduped)
  212. // querying deps with a specific version
  213. #lodash@2.1.5
  214. // equivalent to...
  215. [name="lodash"][version="2.1.5"]
  216. // has any deps
  217. :has(*)
  218. // deps with no other deps (ie. "leaf" nodes)
  219. :empty
  220. // manually querying git dependencies
  221. [repository^=github:],
  222. [repository^=git:],
  223. [repository^=https://github.com],
  224. [repository^=http://github.com],
  225. [repository^=https://github.com],
  226. [repository^=+git:...]
  227. // querying for all git dependencies
  228. :type(git)
  229. // get production dependencies that aren't also dev deps
  230. .prod:not(.dev)
  231. // get dependencies with specific licenses
  232. [license=MIT], [license=ISC]
  233. // find all packages that have @ruyadorno as a contributor
  234. :attr(contributors, [email=ruyadorno@github.com])
  235. </code></pre>
  236. <h3 id="example-response-output">Example Response Output</h3>
  237. <ul>
  238. <li>an array of dependency objects is returned which can contain multiple copies of the same package which may or may not have been linked or deduped</li>
  239. </ul>
  240. <pre><code class="language-json">[
  241. {
  242. "name": "",
  243. "version": "",
  244. "description": "",
  245. "homepage": "",
  246. "bugs": {},
  247. "author": {},
  248. "license": {},
  249. "funding": {},
  250. "files": [],
  251. "main": "",
  252. "browser": "",
  253. "bin": {},
  254. "man": [],
  255. "directories": {},
  256. "repository": {},
  257. "scripts": {},
  258. "config": {},
  259. "dependencies": {},
  260. "devDependencies": {},
  261. "optionalDependencies": {},
  262. "bundledDependencies": {},
  263. "peerDependencies": {},
  264. "peerDependenciesMeta": {},
  265. "engines": {},
  266. "os": [],
  267. "cpu": [],
  268. "workspaces": {},
  269. "keywords": [],
  270. ...
  271. },
  272. ...
  273. </code></pre>
  274. <h3 id="expecting-a-certain-number-of-results">Expecting a certain number of results</h3>
  275. <p>One common use of <code>npm query</code> is to make sure there is only one version of a certain dependency in your tree.
  276. This is especially common for ecosystems like that rely on <code>typescript</code> where having state split across two different but identically-named packages causes bugs.
  277. You can use the <code>--expect-results</code> or <code>--expect-result-count</code> in your setup to ensure that npm will exit with an exit code if your tree doesn't look like you want it to.</p>
  278. <pre><code class="language-sh">$ npm query '#react' --expect-result-count=1
  279. </code></pre>
  280. <p>Perhaps you want to quickly check if there are any production dependencies that could be updated:</p>
  281. <pre><code class="language-sh">$ npm query ':root&gt;:outdated(in-range).prod' --no-expect-results
  282. </code></pre>
  283. <h3 id="package-lock-only-mode">Package lock only mode</h3>
  284. <p>If package-lock-only is enabled, only the information in the package lock (or shrinkwrap) is loaded.
  285. This means that information from the package.json files of your dependencies will not be included in the result set (e.g. description, homepage, engines).</p>
  286. <h3 id="configuration">Configuration</h3>
  287. <h4 id="global"><code>global</code></h4>
  288. <ul>
  289. <li>Default: false</li>
  290. <li>Type: Boolean</li>
  291. </ul>
  292. <p>Operates in "global" mode, so that packages are installed into the <code>prefix</code>
  293. folder instead of the current working directory. See
  294. <a href="../configuring-npm/folders.html">folders</a> for more on the differences in behavior.</p>
  295. <ul>
  296. <li>packages are installed into the <code>{prefix}/lib/node_modules</code> folder, instead
  297. of the current working directory.</li>
  298. <li>bin files are linked to <code>{prefix}/bin</code></li>
  299. <li>man pages are linked to <code>{prefix}/share/man</code></li>
  300. </ul>
  301. <h4 id="workspace"><code>workspace</code></h4>
  302. <ul>
  303. <li>Default:</li>
  304. <li>Type: String (can be set multiple times)</li>
  305. </ul>
  306. <p>Enable running a command in the context of the configured workspaces of the
  307. current project while filtering by running only the workspaces defined by
  308. this configuration option.</p>
  309. <p>Valid values for the <code>workspace</code> config are either:</p>
  310. <ul>
  311. <li>Workspace names</li>
  312. <li>Path to a workspace directory</li>
  313. <li>Path to a parent workspace directory (will result in selecting all
  314. workspaces within that folder)</li>
  315. </ul>
  316. <p>When set for the <code>npm init</code> command, this may be set to the folder of a
  317. workspace which does not yet exist, to create the folder and set it up as a
  318. brand new workspace within the project.</p>
  319. <p>This value is not exported to the environment for child processes.</p>
  320. <h4 id="workspaces"><code>workspaces</code></h4>
  321. <ul>
  322. <li>Default: null</li>
  323. <li>Type: null or Boolean</li>
  324. </ul>
  325. <p>Set to true to run the command in the context of <strong>all</strong> configured
  326. workspaces.</p>
  327. <p>Explicitly setting this to false will cause commands like <code>install</code> to
  328. ignore workspaces altogether. When not set explicitly:</p>
  329. <ul>
  330. <li>Commands that operate on the <code>node_modules</code> tree (install, update, etc.)
  331. will link workspaces into the <code>node_modules</code> folder. - Commands that do
  332. other things (test, exec, publish, etc.) will operate on the root project,
  333. <em>unless</em> one or more workspaces are specified in the <code>workspace</code> config.</li>
  334. </ul>
  335. <p>This value is not exported to the environment for child processes.</p>
  336. <h4 id="include-workspace-root"><code>include-workspace-root</code></h4>
  337. <ul>
  338. <li>Default: false</li>
  339. <li>Type: Boolean</li>
  340. </ul>
  341. <p>Include the workspace root when workspaces are enabled for a command.</p>
  342. <p>When false, specifying individual workspaces via the <code>workspace</code> config, or
  343. all workspaces via the <code>workspaces</code> flag, will cause npm to operate only on
  344. the specified workspaces, and not on the root project.</p>
  345. <p>This value is not exported to the environment for child processes.</p>
  346. <h4 id="package-lock-only"><code>package-lock-only</code></h4>
  347. <ul>
  348. <li>Default: false</li>
  349. <li>Type: Boolean</li>
  350. </ul>
  351. <p>If set to true, the current operation will only use the <code>package-lock.json</code>,
  352. ignoring <code>node_modules</code>.</p>
  353. <p>For <code>update</code> this means only the <code>package-lock.json</code> will be updated,
  354. instead of checking <code>node_modules</code> and downloading dependencies.</p>
  355. <p>For <code>list</code> this means the output will be based on the tree described by the
  356. <code>package-lock.json</code>, rather than the contents of <code>node_modules</code>.</p>
  357. <h4 id="expect-results"><code>expect-results</code></h4>
  358. <ul>
  359. <li>Default: null</li>
  360. <li>Type: null or Boolean</li>
  361. </ul>
  362. <p>Tells npm whether or not to expect results from the command. Can be either
  363. true (expect some results) or false (expect no results).</p>
  364. <p>This config cannot be used with: <code>expect-result-count</code></p>
  365. <h4 id="expect-result-count"><code>expect-result-count</code></h4>
  366. <ul>
  367. <li>Default: null</li>
  368. <li>Type: null or Number</li>
  369. </ul>
  370. <p>Tells to expect a specific number of results from the command.</p>
  371. <p>This config cannot be used with: <code>expect-results</code></p>
  372. <h2 id="see-also">See Also</h2>
  373. <ul>
  374. <li><a href="../using-npm/dependency-selectors.html">dependency selectors</a></li>
  375. </ul></div>
  376. <footer id="edit">
  377. <a href="https://github.com/npm/cli/edit/latest/docs/lib/content/commands/npm-query.md">
  378. <svg role="img" viewBox="0 0 16 16" width="16" height="16" fill="currentcolor" style="vertical-align: text-bottom; margin-right: 0.3em;">
  379. <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>
  380. </svg>
  381. Edit this page on GitHub
  382. </a>
  383. </footer>
  384. </section>
  385. </body></html>