• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 &lt;selector&gt;
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 &amp; uninstall them
156npm query ":attr(scripts, [postinstall])" | jq 'map(.name)|join("\n")' -r | xargs -I {} npm uninstall {}
157
158# find all git dependencies &amp; 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 &amp; Queries</h3>
162<pre><code class="language-stylus">// all deps
163*
164
165// all direct deps
166:root &gt; *
167
168// direct production deps
169:root &gt; .prod
170
171// direct development deps
172:root &gt; .dev
173
174// any peer dep of a direct deps
175:root &gt; * &gt; .peer
176
177// any workspace dep
178.workspace
179
180// all workspaces that depend on another workspace
181.workspace &gt; .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" &amp; 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="configuration">Configuration</h3>
268<h4 id="global"><code>global</code></h4>
269<ul>
270<li>Default: false</li>
271<li>Type: Boolean</li>
272</ul>
273<p>Operates in "global" mode, so that packages are installed into the <code>prefix</code>
274folder instead of the current working directory. See
275<a href="../configuring-npm/folders.html">folders</a> for more on the differences in behavior.</p>
276<ul>
277<li>packages are installed into the <code>{prefix}/lib/node_modules</code> folder, instead
278of the current working directory.</li>
279<li>bin files are linked to <code>{prefix}/bin</code></li>
280<li>man pages are linked to <code>{prefix}/share/man</code></li>
281</ul>
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
288current project while filtering by running only the workspaces defined by
289this 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
295workspaces 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
298workspace which does not yet exist, to create the folder and set it up as a
299brand new workspace within the project.</p>
300<p>This value is not exported to the environment for child processes.</p>
301<h4 id="workspaces"><code>workspaces</code></h4>
302<ul>
303<li>Default: null</li>
304<li>Type: null or Boolean</li>
305</ul>
306<p>Set to true to run the command in the context of <strong>all</strong> configured
307workspaces.</p>
308<p>Explicitly setting this to false will cause commands like <code>install</code> to
309ignore workspaces altogether. When not set explicitly:</p>
310<ul>
311<li>Commands that operate on the <code>node_modules</code> tree (install, update, etc.)
312will link workspaces into the <code>node_modules</code> folder. - Commands that do
313other things (test, exec, publish, etc.) will operate on the root project,
314<em>unless</em> one or more workspaces are specified in the <code>workspace</code> config.</li>
315</ul>
316<p>This value is not exported to the environment for child processes.</p>
317<h4 id="include-workspace-root"><code>include-workspace-root</code></h4>
318<ul>
319<li>Default: false</li>
320<li>Type: Boolean</li>
321</ul>
322<p>Include the workspace root when workspaces are enabled for a command.</p>
323<p>When false, specifying individual workspaces via the <code>workspace</code> config, or
324all workspaces via the <code>workspaces</code> flag, will cause npm to operate only on
325the specified workspaces, and not on the root project.</p>
326<p>This value is not exported to the environment for child processes.</p>
327<h2 id="see-also">See Also</h2>
328<ul>
329<li><a href="../using-npm/dependency-selectors.html">dependency selectors</a></li>
330</ul></div>
331
332<footer id="edit">
333<a href="https://github.com/npm/cli/edit/latest/docs/content/commands/npm-query.md">
334<svg role="img" viewBox="0 0 16 16" width="16" height="16" fill="currentcolor" style="vertical-align: text-bottom; margin-right: 0.3em;">
335<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>
336</svg>
337Edit this page on GitHub
338</a>
339</footer>
340</section>
341
342
343
344</body></html>