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
40 // all workspaces that depend on another workspace
41
42 // all workspaces that have peer deps
43
44 // any dep named "lodash"
45 // equivalent to [name="lodash"]
46 #lodash
47
48 // any deps named "lodash" & within semver range ^"1.2.3"
49 #lodash@^1.2.3
50 // equivalent to...
51 [name="lodash"]:semver(^1.2.3)
52
53 // get the hoisted node for a given semver range
54 #lodash@^1.2.3:not(:deduped)
55
56 // querying deps with a specific version
57 #lodash@2.1.5
58 // equivalent to...
59 [name="lodash"][version="2.1.5"]
60
61 // has any deps
62 :has(*)
63
64 // deps with no other deps (ie. "leaf" nodes)
65 :empty
66
67 // manually querying git dependencies
68 [repository^=github:],
69 [repository^=git:],
70 [repository^=https://github.com],
71 [repository^=http://github.com],
72 [repository^=https://github.com],
73 [repository^=+git:...]
74
75 // querying for all git dependencies
76 :type(git)
77
78 // get production dependencies that aren't also dev deps
79
80 // get dependencies with specific licenses
81 [license=MIT], [license=ISC]
82
83 // find all packages that have @ruyadorno as a contributor
84 :attr(contributors, [email=ruyadorno@github.com])
85
86 Example Response Output
87 • an array of dependency objects is returned which can contain multi‐
88 ple copies of the same package which may or may not have been
89 linked or deduped
90
91
92 [
93 {
94 "name": "",
95 "version": "",
96 "description": "",
97 "homepage": "",
98 "bugs": {},
99 "author": {},
100 "license": {},
101 "funding": {},
102 "files": [],
103 "main": "",
104 "browser": "",
105 "bin": {},
106 "man": [],
107 "directories": {},
108 "repository": {},
109 "scripts": {},
110 "config": {},
111 "dependencies": {},
112 "devDependencies": {},
113 "optionalDependencies": {},
114 "bundledDependencies": {},
115 "peerDependencies": {},
116 "peerDependenciesMeta": {},
117 "engines": {},
118 "os": [],
119 "cpu": [],
120 "workspaces": {},
121 "keywords": [],
122 ...
123 },
124 ...
125
126 Package lock only mode
127 If package-lock-only is enabled, only the information in the package
128 lock (or shrinkwrap) is loaded. This means that information from the
129 package.json files of your dependencies will not be included in the re‐
130 sult set (e.g. description, homepage, engines).
131
132 Package lock only mode
133 If package-lock-only is enabled, only the information in the package
134 lock (or shrinkwrap) is loaded. This means that information from the
135 package.json files of your dependencies will not be included in the re‐
136 sult set (e.g. description, homepage, engines).
137
138 Configuration
139 global
140 • Default: false
141
142 • Type: Boolean
143
144
145 Operates in "global" mode, so that packages are installed into the pre‐
146 fix folder instead of the current working directory. See npm help fold‐
147 ers for more on the differences in behavior.
148
149 • packages are installed into the {prefix}/lib/node_modules folder,
150 instead of the current working directory.
151
152 • bin files are linked to {prefix}/bin
153
154 • man pages are linked to {prefix}/share/man
155
156
157 workspace
158 • Default:
159
160 • Type: String (can be set multiple times)
161
162
163 Enable running a command in the context of the configured workspaces of
164 the current project while filtering by running only the workspaces de‐
165 fined by this configuration option.
166
167 Valid values for the workspace config are either:
168
169 • Workspace names
170
171 • Path to a workspace directory
172
173 • Path to a parent workspace directory (will result in selecting all
174 workspaces within that folder)
175
176
177 When set for the npm init command, this may be set to the folder of a
178 workspace which does not yet exist, to create the folder and set it up
179 as a brand new workspace within the project.
180
181 This value is not exported to the environment for child processes.
182
183 workspaces
184 • Default: null
185
186 • Type: null or Boolean
187
188
189 Set to true to run the command in the context of all configured
190 workspaces.
191
192 Explicitly setting this to false will cause commands like install to
193 ignore workspaces altogether. When not set explicitly:
194
195 • Commands that operate on the node_modules tree (install, update,
196 etc.) will link workspaces into the node_modules folder. - Commands
197 that do other things (test, exec, publish, etc.) will operate on
198 the root project, unless one or more workspaces are specified in
199 the workspace config.
200
201
202 This value is not exported to the environment for child processes.
203
204 include-workspace-root
205 • Default: false
206
207 • Type: Boolean
208
209
210 Include the workspace root when workspaces are enabled for a command.
211
212 When false, specifying individual workspaces via the workspace config,
213 or all workspaces via the workspaces flag, will cause npm to operate
214 only on the specified workspaces, and not on the root project.
215
216 This value is not exported to the environment for child processes.
217
218 package-lock-only
219 • Default: false
220
221 • Type: Boolean
222
223
224 If set to true, the current operation will only use the package-
225 lock.json, ignoring node_modules.
226
227 For update this means only the package-lock.json will be updated, in‐
228 stead of checking node_modules and downloading dependencies.
229
230 For list this means the output will be based on the tree described by
231 the package-lock.json, rather than the contents of node_modules.
232
234 • npm help "dependency selectors"
235
236
237
238 November 2023 NPM-QUERY(1)