dependency-selectors.html 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. <!DOCTYPE html><html><head>
  2. <meta charset="utf-8">
  3. <title>Dependency Selectors</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="----dependency-selectors----11120">
  166. <span>Dependency Selectors</span>
  167. <span class="version">@11.12.0</span>
  168. </h1>
  169. <span class="description">Dependency Selector Syntax &amp; Querying</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="description">Description</h3>
  176. <p>The <a href="../commands/npm-query.html"><code>npm query</code></a> command exposes a new dependency selector syntax (informed by &amp; respecting many aspects of the <a href="https://dev.w3.org/csswg/selectors4/#relational">CSS Selectors 4 Spec</a>) which:</p>
  177. <ul>
  178. <li>Standardizes the shape of, &amp; querying of, dependency graphs with a robust object model, metadata &amp; selector syntax</li>
  179. <li>Leverages existing, known language syntax &amp; operators from CSS to make disparate package information broadly accessible</li>
  180. <li>Unlocks the ability to answer complex, multi-faceted questions about dependencies, their relationships &amp; associative metadata</li>
  181. <li>Consolidates redundant logic of similar query commands in <code>npm</code> (ex.
  182. <code>npm fund</code>, <code>npm ls</code>, <code>npm outdated</code>, <code>npm audit</code> ...)</li>
  183. </ul>
  184. <h3 id="dependency-selector-syntax">Dependency Selector Syntax</h3>
  185. <h4 id="overview">Overview:</h4>
  186. <ul>
  187. <li>there is no "type" or "tag" selectors (ex.
  188. <code>div, h1, a</code>) as a dependency/target is the only type of <code>Node</code> that can be queried</li>
  189. <li>the term "dependencies" is in reference to any <code>Node</code> found in a <code>tree</code> returned by <code>Arborist</code></li>
  190. </ul>
  191. <h4 id="combinators">Combinators</h4>
  192. <ul>
  193. <li><code>&gt;</code> direct descendant/child</li>
  194. <li><code> </code> any descendant/child</li>
  195. <li><code>~</code> sibling</li>
  196. </ul>
  197. <h4 id="selectors">Selectors</h4>
  198. <ul>
  199. <li><code>*</code> universal selector</li>
  200. <li><code>#&lt;name&gt;</code> dependency selector (equivalent to <code>[name="..."]</code>)</li>
  201. <li><code>#&lt;name&gt;@&lt;version&gt;</code> (equivalent to <code>[name=&lt;name&gt;]:semver(&lt;version&gt;)</code>)</li>
  202. <li><code>,</code> selector list delimiter</li>
  203. <li><code>.</code> dependency type selector</li>
  204. <li><code>:</code> pseudo selector</li>
  205. </ul>
  206. <h4 id="dependency-type-selectors">Dependency Type Selectors</h4>
  207. <ul>
  208. <li><code>.prod</code> dependency found in the <code>dependencies</code> section of <code>package.json</code>, or is a child of said dependency</li>
  209. <li><code>.dev</code> dependency found in the <code>devDependencies</code> section of <code>package.json</code>, or is a child of said dependency</li>
  210. <li><code>.optional</code> dependency found in the <code>optionalDependencies</code> section of <code>package.json</code>, or has <code>"optional": true</code> set in its entry in the <code>peerDependenciesMeta</code> section of <code>package.json</code>, or a child of said dependency</li>
  211. <li><code>.peer</code> dependency found in the <code>peerDependencies</code> section of <code>package.json</code></li>
  212. <li><code>.workspace</code> dependency found in the <a href="https://docs.npmjs.com/cli/v8/using-npm/workspaces"><code>workspaces</code></a> section of <code>package.json</code></li>
  213. <li><code>.bundled</code> dependency found in the <code>bundleDependencies</code> section of <code>package.json</code>, or is a child of said dependency</li>
  214. </ul>
  215. <h4 id="pseudo-selectors">Pseudo Selectors</h4>
  216. <ul>
  217. <li><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:not"><code>:not(&lt;selector&gt;)</code></a></li>
  218. <li><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:has"><code>:has(&lt;selector&gt;)</code></a></li>
  219. <li><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:is"><code>:is(&lt;selector list&gt;)</code></a></li>
  220. <li><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:root"><code>:root</code></a> matches the root node/dependency</li>
  221. <li><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:scope"><code>:scope</code></a> matches node/dependency it was queried against</li>
  222. <li><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:empty"><code>:empty</code></a> when a dependency has no dependencies</li>
  223. <li><a href="https://docs.npmjs.com/cli/v8/configuring-npm/package-json#private"><code>:private</code></a> when a dependency is private</li>
  224. <li><code>:link</code> when a dependency is linked (for instance, workspaces or packages manually <a href="https://docs.npmjs.com/cli/v8/commands/npm-link"><code>linked</code></a></li>
  225. <li><code>:deduped</code> when a dependency has been deduped (note that this does <em>not</em> always mean the dependency has been hoisted to the root of node_modules)</li>
  226. <li><code>:overridden</code> when a dependency has been overridden</li>
  227. <li><code>:extraneous</code> when a dependency exists but is not defined as a dependency of any node</li>
  228. <li><code>:invalid</code> when a dependency version is out of its ancestors specified range</li>
  229. <li><code>:missing</code> when a dependency is not found on disk</li>
  230. <li><code>:semver(&lt;spec&gt;, [selector], [function])</code> match a valid <a href="https://github.com/npm/node-semver"><code>node-semver</code></a> version or range to a selector</li>
  231. <li><code>:path(&lt;path&gt;)</code> <a href="https://www.npmjs.com/package/glob">glob</a> matching based on dependencies path relative to the project</li>
  232. <li><code>:type(&lt;type&gt;)</code> <a href="https://github.com/npm/npm-package-arg#result-object">based on currently recognized types</a>. You can also use the aggregate type of <code>registry</code> for any registry dependency (e.g. tag, version, range, alias)</li>
  233. <li><code>:outdated(&lt;type&gt;)</code> when a dependency is outdated</li>
  234. <li><code>:vuln(&lt;selector&gt;)</code> when a dependency has a known vulnerability</li>
  235. </ul>
  236. <h5 id="semverspec-selector-function"><code>:semver(&lt;spec&gt;, [selector], [function])</code></h5>
  237. <p>The <code>:semver()</code> pseudo selector allows comparing fields from each node's <code>package.json</code> using <a href="https://github.com/npm/node-semver#readme">semver</a> methods.
  238. It accepts up to 3 parameters, all but the first of which are optional.</p>
  239. <ul>
  240. <li><code>spec</code> a semver version or range</li>
  241. <li><code>selector</code> an attribute selector for each node (default <code>[version]</code>)</li>
  242. <li><code>function</code> a semver method to apply, one of: <code>satisfies</code>, <code>intersects</code>, <code>subset</code>, <code>gt</code>, <code>gte</code>, <code>gtr</code>, <code>lt</code>, <code>lte</code>, <code>ltr</code>, <code>eq</code>, <code>neq</code> or the special function <code>infer</code> (default <code>infer</code>)</li>
  243. </ul>
  244. <p>When the special <code>infer</code> function is used the <code>spec</code> and the actual value from the node are compared.
  245. If both are versions, according to <code>semver.valid()</code>, <code>eq</code> is used.
  246. If both values are ranges, according to <code>!semver.valid()</code>, <code>intersects</code> is used.
  247. If the values are mixed types <code>satisfies</code> is used.</p>
  248. <p>Some examples:</p>
  249. <ul>
  250. <li><code>:semver(^1.0.0)</code> returns every node that has a <code>version</code> satisfied by the provided range <code>^1.0.0</code></li>
  251. <li><code>:semver(16.0.0, :attr(engines, [node]))</code> returns every node which has an <code>engines.node</code> property satisfying the version <code>16.0.0</code></li>
  252. <li><code>:semver(1.0.0, [version], lt)</code> every node with a <code>version</code> less than <code>1.0.0</code></li>
  253. </ul>
  254. <h5 id="outdatedtype"><code>:outdated(&lt;type&gt;)</code></h5>
  255. <p>The <code>:outdated</code> pseudo selector retrieves data from the registry and returns information about which of your dependencies are outdated.
  256. The type parameter may be one of the following:</p>
  257. <ul>
  258. <li><code>any</code> (default) a version exists that is greater than the current one</li>
  259. <li><code>in-range</code> a version exists that is greater than the current one, and satisfies at least one if its parent's dependencies</li>
  260. <li><code>out-of-range</code> a version exists that is greater than the current one, does not satisfy at least one of its parent's dependencies</li>
  261. <li><code>major</code> a version exists that is a semver major greater than the current one</li>
  262. <li><code>minor</code> a version exists that is a semver minor greater than the current one</li>
  263. <li><code>patch</code> a version exists that is a semver patch greater than the current one</li>
  264. </ul>
  265. <p>In addition to the filtering performed by the pseudo selector, some extra data is added to the resulting objects.
  266. The following data can be found under the <code>queryContext</code> property of each node.</p>
  267. <ul>
  268. <li><code>versions</code> an array of every available version of the given node</li>
  269. <li><code>outdated.inRange</code> an array of objects, each with a <code>from</code> and <code>versions</code>, where <code>from</code> is the on-disk location of the node that depends on the current node and <code>versions</code> is an array of all available versions that satisfies that dependency.
  270. This is only populated if <code>:outdated(in-range)</code> is used.</li>
  271. <li><code>outdated.outOfRange</code> an array of objects, identical in shape to <code>inRange</code>, but where the <code>versions</code> array is every available version that does not satisfy the dependency.
  272. This is only populated if <code>:outdated(out-of-range)</code> is used.</li>
  273. </ul>
  274. <p>Some examples:</p>
  275. <ul>
  276. <li><code>:root &gt; :outdated(major)</code> returns every direct dependency that has a new semver major release</li>
  277. <li><code>.prod:outdated(in-range)</code> returns production dependencies that have a new release that satisfies at least one of its parent's dependencies</li>
  278. </ul>
  279. <h5 id="vuln"><code>:vuln</code></h5>
  280. <p>The <code>:vuln</code> pseudo selector retrieves data from the registry and returns information about which if your dependencies has a known vulnerability.
  281. Only dependencies whose current version matches a vulnerability will be returned.
  282. For example if you have <code>semver@7.6.0</code> in your tree, a vulnerability for <code>semver</code> which affects versions <code>&lt;=6.3.1</code> will not match.</p>
  283. <p>You can also filter results by certain attributes in advisories.
  284. Currently that includes <code>severity</code> and <code>cwe</code>.
  285. Note that severity filtering is done per severity, it does not include severities "higher" or "lower" than the one specified.</p>
  286. <p>In addition to the filtering performed by the pseudo selector, info about each relevant advisory will be added to the <code>queryContext</code> attribute of each node under the <code>advisories</code> attribute.</p>
  287. <p>Some examples:</p>
  288. <ul>
  289. <li><code>:root &gt; .prod:vuln</code> returns direct production dependencies with any known vulnerability</li>
  290. <li><code>:vuln([severity=high])</code> returns only dependencies with a vulnerability with a <code>high</code> severity.</li>
  291. <li><code>:vuln([severity=high],[severity=moderate])</code> returns only dependencies with a vulnerability with a <code>high</code> or <code>moderate</code> severity.</li>
  292. <li><code>:vuln([cwe=1333])</code> returns only dependencies with a vulnerability that includes CWE-1333 (ReDoS)</li>
  293. </ul>
  294. <h4 id="attribute-selectors"><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors">Attribute Selectors</a></h4>
  295. <p>The attribute selector evaluates the key/value pairs in <code>package.json</code> if they are <code>String</code>s.</p>
  296. <ul>
  297. <li><code>[]</code> attribute selector (ie.
  298. existence of attribute)</li>
  299. <li><code>[attribute=value]</code> attribute value is equivalent...</li>
  300. <li><code>[attribute~=value]</code> attribute value contains word...</li>
  301. <li><code>[attribute*=value]</code> attribute value contains string...</li>
  302. <li><code>[attribute|=value]</code> attribute value is equal to or starts with...</li>
  303. <li><code>[attribute^=value]</code> attribute value starts with...</li>
  304. <li><code>[attribute$=value]</code> attribute value ends with...</li>
  305. </ul>
  306. <h4 id="array--object-attribute-selectors"><code>Array</code> &amp; <code>Object</code> Attribute Selectors</h4>
  307. <p>The generic <code>:attr()</code> pseudo selector standardizes a pattern which can be used for attribute selection of <code>Object</code>s, <code>Array</code>s or <code>Arrays</code> of <code>Object</code>s accessible via <code>Arborist</code>'s <code>Node.package</code> metadata.
  308. This allows for iterative attribute selection beyond top-level <code>String</code> evaluation.
  309. The last argument passed to <code>:attr()</code> must be an <code>attribute</code> selector or a nested <code>:attr()</code>.
  310. See examples below:</p>
  311. <h4 id="objects"><code>Objects</code></h4>
  312. <pre><code class="language-css">/* return dependencies that have a `scripts.test` containing `"tap"` */
  313. *:attr(scripts, [test~=tap])
  314. </code></pre>
  315. <h4 id="nested-objects">Nested <code>Objects</code></h4>
  316. <p>Nested objects are expressed as sequential arguments to <code>:attr()</code>.</p>
  317. <pre><code class="language-css">/* return dependencies that have a [testling config](https://ci.testling.com/guide/advanced_configuration) for opera browsers */
  318. *:attr(testling, browsers, [~=opera])
  319. </code></pre>
  320. <h4 id="arrays"><code>Arrays</code></h4>
  321. <p><code>Array</code>s specifically uses a special/reserved <code>.</code> character in place of a typical attribute name.
  322. <code>Arrays</code> also support exact <code>value</code> matching when a <code>String</code> is passed to the selector.</p>
  323. <h5 id="example-of-an-array-attribute-selection">Example of an <code>Array</code> Attribute Selection:</h5>
  324. <pre><code class="language-css">/* removes the distinction between properties &amp; arrays */
  325. /* ie. we'd have to check the property &amp; iterate to match selection */
  326. *:attr([keywords^=react])
  327. *:attr(contributors, :attr([name~=Jordan]))
  328. </code></pre>
  329. <h5 id="example-of-an-array-matching-directly-to-a-value">Example of an <code>Array</code> matching directly to a value:</h5>
  330. <pre><code class="language-css">/* return dependencies that have the exact keyword "react" */
  331. /* this is equivalent to `*:keywords([value="react"])` */
  332. *:attr([keywords=react])
  333. </code></pre>
  334. <h5 id="example-of-an-array-of-objects">Example of an <code>Array</code> of <code>Object</code>s:</h5>
  335. <pre><code class="language-css">/* returns */
  336. *:attr(contributors, [email=ruyadorno@github.com])
  337. </code></pre>
  338. <h3 id="groups">Groups</h3>
  339. <p>Dependency groups are defined by the package relationships to their ancestors (ie.
  340. the dependency types that are defined in <code>package.json</code>).
  341. This approach is user-centric as the ecosystem has been taught to think about dependencies in these groups first-and-foremost.
  342. Dependencies are allowed to be included in multiple groups (ex.
  343. a <code>prod</code> dependency may also be a <code>dev</code> dependency (in that it's also required by another <code>dev</code> dependency) &amp; may also be <code>bundled</code> - a selector for that type of dependency would look like: <code>*.prod.dev.bundled</code>).</p>
  344. <ul>
  345. <li><code>.prod</code></li>
  346. <li><code>.dev</code></li>
  347. <li><code>.optional</code></li>
  348. <li><code>.peer</code></li>
  349. <li><code>.bundled</code></li>
  350. <li><code>.workspace</code></li>
  351. </ul>
  352. <p>Please note that currently <code>workspace</code> deps are always <code>prod</code> dependencies.
  353. Additionally the <code>.root</code> dependency is also considered a <code>prod</code> dependency.</p>
  354. <h3 id="programmatic-usage">Programmatic Usage</h3>
  355. <ul>
  356. <li><code>Arborist</code>'s <code>Node</code> Class has a <code>.querySelectorAll()</code> method
  357. <ul>
  358. <li>this method will return a filtered, flattened dependency Arborist <code>Node</code> list based on a valid query selector</li>
  359. </ul>
  360. </li>
  361. </ul>
  362. <pre><code class="language-js">const Arborist = require('@npmcli/arborist')
  363. const arb = new Arborist({})
  364. </code></pre>
  365. <pre><code class="language-js">// root-level
  366. arb.loadActual().then(async (tree) =&gt; {
  367. // query all production dependencies
  368. const results = await tree.querySelectorAll('.prod')
  369. console.log(results)
  370. })
  371. </code></pre>
  372. <pre><code class="language-js">// iterative
  373. arb.loadActual().then(async (tree) =&gt; {
  374. // query for the deduped version of react
  375. const results = await tree.querySelectorAll('#react:not(:deduped)')
  376. // query the deduped react for git deps
  377. const deps = await results[0].querySelectorAll(':type(git)')
  378. console.log(deps)
  379. })
  380. </code></pre>
  381. <h2 id="see-also">See Also</h2>
  382. <ul>
  383. <li><a href="../commands/npm-query.html">npm query</a></li>
  384. <li><a href="https://npm.im/@npmcli/arborist">@npmcli/arborist</a></li>
  385. </ul></div>
  386. <footer id="edit">
  387. <a href="https://github.com/npm/cli/edit/latest/docs/lib/content/using-npm/dependency-selectors.md">
  388. <svg role="img" viewBox="0 0 16 16" width="16" height="16" fill="currentcolor" style="vertical-align: text-bottom; margin-right: 0.3em;">
  389. <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>
  390. </svg>
  391. Edit this page on GitHub
  392. </a>
  393. </footer>
  394. </section>
  395. </body></html>