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