npm-query.1 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. .TH "NPM-QUERY" "1" "March 2026" "NPM@11.12.0" ""
  2. .SH "NAME"
  3. \fBnpm-query\fR - Dependency selector query
  4. .SS "Synopsis"
  5. .P
  6. .RS 2
  7. .nf
  8. npm query <selector>
  9. .fi
  10. .RE
  11. .SS "Description"
  12. .P
  13. The \fBnpm query\fR command allows for usage of css selectors in order to retrieve an array of dependency objects.
  14. .SS "Piping npm query to other commands"
  15. .P
  16. .RS 2
  17. .nf
  18. # find all dependencies with postinstall scripts & uninstall them
  19. npm query ":attr(scripts, \[lB]postinstall\[rB])" | jq 'map(.name)|join("\[rs]n")' -r | xargs -I {} npm uninstall {}
  20. # find all git dependencies & explain who requires them
  21. npm query ":type(git)" | jq 'map(.name)' | xargs -I {} npm why {}
  22. .fi
  23. .RE
  24. .SS "Extended Use Cases & Queries"
  25. .P
  26. .RS 2
  27. .nf
  28. // all deps
  29. *
  30. // all direct deps
  31. :root > *
  32. // direct production deps
  33. :root > .prod
  34. // direct development deps
  35. :root > .dev
  36. // any peer dep of a direct deps
  37. :root > * > .peer
  38. // any workspace dep
  39. .workspace
  40. // all workspaces that depend on another workspace
  41. .workspace > .workspace
  42. // all workspaces that have peer deps
  43. .workspace:has(.peer)
  44. // any dep named "lodash"
  45. // equivalent to \[lB]name="lodash"\[rB]
  46. #lodash
  47. // any deps named "lodash" & within semver range ^"1.2.3"
  48. #lodash@^1.2.3
  49. // equivalent to...
  50. \[lB]name="lodash"\[rB]:semver(^1.2.3)
  51. // get the hoisted node for a given semver range
  52. #lodash@^1.2.3:not(:deduped)
  53. // querying deps with a specific version
  54. #lodash@2.1.5
  55. // equivalent to...
  56. \[lB]name="lodash"\[rB]\[lB]version="2.1.5"\[rB]
  57. // has any deps
  58. :has(*)
  59. // deps with no other deps (ie. "leaf" nodes)
  60. :empty
  61. // manually querying git dependencies
  62. \[lB]repository^=github:\[rB],
  63. \[lB]repository^=git:\[rB],
  64. \[lB]repository^=https://github.com\[rB],
  65. \[lB]repository^=http://github.com\[rB],
  66. \[lB]repository^=https://github.com\[rB],
  67. \[lB]repository^=+git:...\[rB]
  68. // querying for all git dependencies
  69. :type(git)
  70. // get production dependencies that aren't also dev deps
  71. .prod:not(.dev)
  72. // get dependencies with specific licenses
  73. \[lB]license=MIT\[rB], \[lB]license=ISC\[rB]
  74. // find all packages that have @ruyadorno as a contributor
  75. :attr(contributors, \[lB]email=ruyadorno@github.com\[rB])
  76. .fi
  77. .RE
  78. .SS "Example Response Output"
  79. .RS 0
  80. .IP \(bu 4
  81. 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
  82. .RE 0
  83. .P
  84. .RS 2
  85. .nf
  86. \[lB]
  87. {
  88. "name": "",
  89. "version": "",
  90. "description": "",
  91. "homepage": "",
  92. "bugs": {},
  93. "author": {},
  94. "license": {},
  95. "funding": {},
  96. "files": \[lB]\[rB],
  97. "main": "",
  98. "browser": "",
  99. "bin": {},
  100. "man": \[lB]\[rB],
  101. "directories": {},
  102. "repository": {},
  103. "scripts": {},
  104. "config": {},
  105. "dependencies": {},
  106. "devDependencies": {},
  107. "optionalDependencies": {},
  108. "bundledDependencies": {},
  109. "peerDependencies": {},
  110. "peerDependenciesMeta": {},
  111. "engines": {},
  112. "os": \[lB]\[rB],
  113. "cpu": \[lB]\[rB],
  114. "workspaces": {},
  115. "keywords": \[lB]\[rB],
  116. ...
  117. },
  118. ...
  119. .fi
  120. .RE
  121. .SS "Expecting a certain number of results"
  122. .P
  123. One common use of \fBnpm query\fR is to make sure there is only one version of a certain dependency in your tree. This is especially common for ecosystems like that rely on \fBtypescript\fR where having state split across two different but identically-named packages causes bugs. You can use the \fB--expect-results\fR or \fB--expect-result-count\fR in your setup to ensure that npm will exit with an exit code if your tree doesn't look like you want it to.
  124. .P
  125. .RS 2
  126. .nf
  127. $ npm query '#react' --expect-result-count=1
  128. .fi
  129. .RE
  130. .P
  131. Perhaps you want to quickly check if there are any production dependencies that could be updated:
  132. .P
  133. .RS 2
  134. .nf
  135. $ npm query ':root>:outdated(in-range).prod' --no-expect-results
  136. .fi
  137. .RE
  138. .SS "Package lock only mode"
  139. .P
  140. 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).
  141. .SS "Configuration"
  142. .SS "\fBglobal\fR"
  143. .RS 0
  144. .IP \(bu 4
  145. Default: false
  146. .IP \(bu 4
  147. Type: Boolean
  148. .RE 0
  149. .P
  150. Operates in "global" mode, so that packages are installed into the \fBprefix\fR folder instead of the current working directory. See npm help folders for more on the differences in behavior.
  151. .RS 0
  152. .IP \(bu 4
  153. packages are installed into the \fB{prefix}/lib/node_modules\fR folder, instead of the current working directory.
  154. .IP \(bu 4
  155. bin files are linked to \fB{prefix}/bin\fR
  156. .IP \(bu 4
  157. man pages are linked to \fB{prefix}/share/man\fR
  158. .RE 0
  159. .SS "\fBworkspace\fR"
  160. .RS 0
  161. .IP \(bu 4
  162. Default:
  163. .IP \(bu 4
  164. Type: String (can be set multiple times)
  165. .RE 0
  166. .P
  167. 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.
  168. .P
  169. Valid values for the \fBworkspace\fR config are either:
  170. .RS 0
  171. .IP \(bu 4
  172. Workspace names
  173. .IP \(bu 4
  174. Path to a workspace directory
  175. .IP \(bu 4
  176. Path to a parent workspace directory (will result in selecting all workspaces within that folder)
  177. .RE 0
  178. .P
  179. When set for the \fBnpm init\fR 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.
  180. .P
  181. This value is not exported to the environment for child processes.
  182. .SS "\fBworkspaces\fR"
  183. .RS 0
  184. .IP \(bu 4
  185. Default: null
  186. .IP \(bu 4
  187. Type: null or Boolean
  188. .RE 0
  189. .P
  190. Set to true to run the command in the context of \fBall\fR configured workspaces.
  191. .P
  192. Explicitly setting this to false will cause commands like \fBinstall\fR to ignore workspaces altogether. When not set explicitly:
  193. .RS 0
  194. .IP \(bu 4
  195. Commands that operate on the \fBnode_modules\fR tree (install, update, etc.) will link workspaces into the \fBnode_modules\fR folder. - Commands that do other things (test, exec, publish, etc.) will operate on the root project, \fIunless\fR one or more workspaces are specified in the \fBworkspace\fR config.
  196. .RE 0
  197. .P
  198. This value is not exported to the environment for child processes.
  199. .SS "\fBinclude-workspace-root\fR"
  200. .RS 0
  201. .IP \(bu 4
  202. Default: false
  203. .IP \(bu 4
  204. Type: Boolean
  205. .RE 0
  206. .P
  207. Include the workspace root when workspaces are enabled for a command.
  208. .P
  209. When false, specifying individual workspaces via the \fBworkspace\fR config, or all workspaces via the \fBworkspaces\fR flag, will cause npm to operate only on the specified workspaces, and not on the root project.
  210. .P
  211. This value is not exported to the environment for child processes.
  212. .SS "\fBpackage-lock-only\fR"
  213. .RS 0
  214. .IP \(bu 4
  215. Default: false
  216. .IP \(bu 4
  217. Type: Boolean
  218. .RE 0
  219. .P
  220. If set to true, the current operation will only use the \fBpackage-lock.json\fR, ignoring \fBnode_modules\fR.
  221. .P
  222. For \fBupdate\fR this means only the \fBpackage-lock.json\fR will be updated, instead of checking \fBnode_modules\fR and downloading dependencies.
  223. .P
  224. For \fBlist\fR this means the output will be based on the tree described by the \fBpackage-lock.json\fR, rather than the contents of \fBnode_modules\fR.
  225. .SS "\fBexpect-results\fR"
  226. .RS 0
  227. .IP \(bu 4
  228. Default: null
  229. .IP \(bu 4
  230. Type: null or Boolean
  231. .RE 0
  232. .P
  233. Tells npm whether or not to expect results from the command. Can be either true (expect some results) or false (expect no results).
  234. .P
  235. This config cannot be used with: \fBexpect-result-count\fR
  236. .SS "\fBexpect-result-count\fR"
  237. .RS 0
  238. .IP \(bu 4
  239. Default: null
  240. .IP \(bu 4
  241. Type: null or Number
  242. .RE 0
  243. .P
  244. Tells to expect a specific number of results from the command.
  245. .P
  246. This config cannot be used with: \fBexpect-results\fR
  247. .SH "SEE ALSO"
  248. .RS 0
  249. .IP \(bu 4
  250. npm help "dependency selectors"
  251. .RE 0