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