1NPM-QUERY(1) NPM-QUERY(1)
2
3
4
6 npm-query - Dependency selector query
7
8 Synopsis
9 npm query <selector>
10
11 Description
12 The npm query command allows for usage of css selectors in order to re‐
13 trieve an array of dependency objects.
14
15 Piping npm query to other commands
16 # find all dependencies with postinstall scripts & uninstall them
17 npm query ":attr(scripts, [postinstall])" | jq 'map(.name)|join("\n")' -r | xargs -I {} npm uninstall {}
18
19 # find all git dependencies & explain who requires them
20 npm query ":type(git)" | jq 'map(.name)' | xargs -I {} npm why {}
21
22 Extended Use Cases & Queries
23 // all deps
24 *
25
26 // all direct deps
27 :root > *
28
29 // direct production deps
30 :root > .prod
31
32 // direct development deps
33 :root > .dev
34
35 // any peer dep of a direct deps
36 :root > * > .peer
37
38 // any workspace dep
39 .workspace
40
41 // all workspaces that depend on another workspace
42 .workspace > .workspace
43
44 // all workspaces that have peer deps
45 .workspace:has(.peer)
46
47 // any dep named "lodash"
48 // equivalent to [name="lodash"]
49 #lodash
50
51 // any deps named "lodash" & within semver range ^"1.2.3"
52 #lodash@^1.2.3
53 // equivalent to...
54 [name="lodash"]:semver(^1.2.3)
55
56 // get the hoisted node for a given semver range
57 #lodash@^1.2.3:not(:deduped)
58
59 // querying deps with a specific version
60 #lodash@2.1.5
61 // equivalent to...
62 [name="lodash"][version="2.1.5"]
63
64 // has any deps
65 :has(*)
66
67 // deps with no other deps (ie. "leaf" nodes)
68 :empty
69
70 // manually querying git dependencies
71 [repository^=github:],
72 [repository^=git:],
73 [repository^=https://github.com],
74 [repository^=http://github.com],
75 [repository^=https://github.com],
76 [repository^=+git:...]
77
78 // querying for all git dependencies
79 :type(git)
80
81 // get production dependencies that aren't also dev deps
82 .prod:not(.dev)
83
84 // get dependencies with specific licenses
85 [license=MIT], [license=ISC]
86
87 // find all packages that have @ruyadorno as a contributor
88 :attr(contributors, [email=ruyadorno@github.com])
89
90 Example Response Output
91 • an array of dependency objects is returned which can contain multiple
92 copies of the same package which may or may not have been linked or
93 deduped
94
95
96 [
97 {
98 "name": "",
99 "version": "",
100 "description": "",
101 "homepage": "",
102 "bugs": {},
103 "author": {},
104 "license": {},
105 "funding": {},
106 "files": [],
107 "main": "",
108 "browser": "",
109 "bin": {},
110 "man": [],
111 "directories": {},
112 "repository": {},
113 "scripts": {},
114 "config": {},
115 "dependencies": {},
116 "devDependencies": {},
117 "optionalDependencies": {},
118 "bundledDependencies": {},
119 "peerDependencies": {},
120 "peerDependenciesMeta": {},
121 "engines": {},
122 "os": [],
123 "cpu": [],
124 "workspaces": {},
125 "keywords": [],
126 ...
127 },
128 ...
129
130 Configuration
131 global
132 • Default: false
133
134 • Type: Boolean
135
136
137 Operates in "global" mode, so that packages are installed into the pre‐
138 fix folder instead of the current working directory. See npm help fold‐
139 ers for more on the differences in behavior.
140
141 • packages are installed into the {prefix}/lib/node_modules folder, in‐
142 stead of the current working directory.
143
144 • bin files are linked to {prefix}/bin
145
146 • man pages are linked to {prefix}/share/man
147
148
149 workspace
150 • Default:
151
152 • Type: String (can be set multiple times)
153
154
155 Enable running a command in the context of the configured workspaces of
156 the current project while filtering by running only the workspaces de‐
157 fined by this configuration option.
158
159 Valid values for the workspace config are either:
160
161 • Workspace names
162
163 • Path to a workspace directory
164
165 • Path to a parent workspace directory (will result in selecting all
166 workspaces within that folder)
167
168
169 When set for the npm init command, this may be set to the folder of a
170 workspace which does not yet exist, to create the folder and set it up
171 as a brand new workspace within the project.
172
173 This value is not exported to the environment for child processes.
174
175 workspaces
176 • Default: null
177
178 • Type: null or Boolean
179
180
181 Set to true to run the command in the context of all configured
182 workspaces.
183
184 Explicitly setting this to false will cause commands like install to
185 ignore workspaces altogether. When not set explicitly:
186
187 • Commands that operate on the node_modules tree (install, update,
188 etc.) will link workspaces into the node_modules folder. - Commands
189 that do other things (test, exec, publish, etc.) will operate on the
190 root project, unless one or more workspaces are specified in the
191 workspace config.
192
193
194 This value is not exported to the environment for child processes.
195
196 include-workspace-root
197 • Default: false
198
199 • Type: Boolean
200
201
202 Include the workspace root when workspaces are enabled for a command.
203
204 When false, specifying individual workspaces via the workspace config,
205 or all workspaces via the workspaces flag, will cause npm to operate
206 only on the specified workspaces, and not on the root project.
207
208 This value is not exported to the environment for child processes.
209
211 • npm help dependency selectors
212
213
214
215
216 September 2022 NPM-QUERY(1)