11cb0ef41Sopenharmony_ci---
21cb0ef41Sopenharmony_cititle: npm-query
31cb0ef41Sopenharmony_cisection: 1
41cb0ef41Sopenharmony_cidescription: Dependency selector query
51cb0ef41Sopenharmony_ci---
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci### Synopsis
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci```bash
101cb0ef41Sopenharmony_cinpm query <selector>
111cb0ef41Sopenharmony_ci```
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci### Description
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciThe `npm query` command allows for usage of css selectors in order to retrieve
161cb0ef41Sopenharmony_cian array of dependency objects.
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci### Piping npm query to other commands
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci```bash
211cb0ef41Sopenharmony_ci# find all dependencies with postinstall scripts & uninstall them
221cb0ef41Sopenharmony_cinpm query ":attr(scripts, [postinstall])" | jq 'map(.name)|join("\n")' -r | xargs -I {} npm uninstall {}
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci# find all git dependencies & explain who requires them
251cb0ef41Sopenharmony_cinpm query ":type(git)" | jq 'map(.name)' | xargs -I {} npm why {}
261cb0ef41Sopenharmony_ci```
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci### Extended Use Cases & Queries
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci```stylus
311cb0ef41Sopenharmony_ci// all deps
321cb0ef41Sopenharmony_ci*
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci// all direct deps
351cb0ef41Sopenharmony_ci:root > *
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci// direct production deps
381cb0ef41Sopenharmony_ci:root > .prod
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci// direct development deps
411cb0ef41Sopenharmony_ci:root > .dev
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci// any peer dep of a direct deps
441cb0ef41Sopenharmony_ci:root > * > .peer
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci// any workspace dep
471cb0ef41Sopenharmony_ci.workspace
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci// all workspaces that depend on another workspace
501cb0ef41Sopenharmony_ci.workspace > .workspace
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci// all workspaces that have peer deps
531cb0ef41Sopenharmony_ci.workspace:has(.peer)
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci// any dep named "lodash"
561cb0ef41Sopenharmony_ci// equivalent to [name="lodash"]
571cb0ef41Sopenharmony_ci#lodash
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci// any deps named "lodash" & within semver range ^"1.2.3"
601cb0ef41Sopenharmony_ci#lodash@^1.2.3
611cb0ef41Sopenharmony_ci// equivalent to...
621cb0ef41Sopenharmony_ci[name="lodash"]:semver(^1.2.3)
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci// get the hoisted node for a given semver range
651cb0ef41Sopenharmony_ci#lodash@^1.2.3:not(:deduped)
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci// querying deps with a specific version
681cb0ef41Sopenharmony_ci#lodash@2.1.5
691cb0ef41Sopenharmony_ci// equivalent to...
701cb0ef41Sopenharmony_ci[name="lodash"][version="2.1.5"]
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci// has any deps
731cb0ef41Sopenharmony_ci:has(*)
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci// deps with no other deps (ie. "leaf" nodes)
761cb0ef41Sopenharmony_ci:empty
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci// manually querying git dependencies
791cb0ef41Sopenharmony_ci[repository^=github:],
801cb0ef41Sopenharmony_ci[repository^=git:],
811cb0ef41Sopenharmony_ci[repository^=https://github.com],
821cb0ef41Sopenharmony_ci[repository^=http://github.com],
831cb0ef41Sopenharmony_ci[repository^=https://github.com],
841cb0ef41Sopenharmony_ci[repository^=+git:...]
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci// querying for all git dependencies
871cb0ef41Sopenharmony_ci:type(git)
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci// get production dependencies that aren't also dev deps
901cb0ef41Sopenharmony_ci.prod:not(.dev)
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci// get dependencies with specific licenses
931cb0ef41Sopenharmony_ci[license=MIT], [license=ISC]
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ci// find all packages that have @ruyadorno as a contributor
961cb0ef41Sopenharmony_ci:attr(contributors, [email=ruyadorno@github.com])
971cb0ef41Sopenharmony_ci```
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_ci### Example Response Output
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ci- 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
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_ci```json
1041cb0ef41Sopenharmony_ci[
1051cb0ef41Sopenharmony_ci  {
1061cb0ef41Sopenharmony_ci    "name": "",
1071cb0ef41Sopenharmony_ci    "version": "",
1081cb0ef41Sopenharmony_ci    "description": "",
1091cb0ef41Sopenharmony_ci    "homepage": "",
1101cb0ef41Sopenharmony_ci    "bugs": {},
1111cb0ef41Sopenharmony_ci    "author": {},
1121cb0ef41Sopenharmony_ci    "license": {},
1131cb0ef41Sopenharmony_ci    "funding": {},
1141cb0ef41Sopenharmony_ci    "files": [],
1151cb0ef41Sopenharmony_ci    "main": "",
1161cb0ef41Sopenharmony_ci    "browser": "",
1171cb0ef41Sopenharmony_ci    "bin": {},
1181cb0ef41Sopenharmony_ci    "man": [],
1191cb0ef41Sopenharmony_ci    "directories": {},
1201cb0ef41Sopenharmony_ci    "repository": {},
1211cb0ef41Sopenharmony_ci    "scripts": {},
1221cb0ef41Sopenharmony_ci    "config": {},
1231cb0ef41Sopenharmony_ci    "dependencies": {},
1241cb0ef41Sopenharmony_ci    "devDependencies": {},
1251cb0ef41Sopenharmony_ci    "optionalDependencies": {},
1261cb0ef41Sopenharmony_ci    "bundledDependencies": {},
1271cb0ef41Sopenharmony_ci    "peerDependencies": {},
1281cb0ef41Sopenharmony_ci    "peerDependenciesMeta": {},
1291cb0ef41Sopenharmony_ci    "engines": {},
1301cb0ef41Sopenharmony_ci    "os": [],
1311cb0ef41Sopenharmony_ci    "cpu": [],
1321cb0ef41Sopenharmony_ci    "workspaces": {},
1331cb0ef41Sopenharmony_ci    "keywords": [],
1341cb0ef41Sopenharmony_ci    ...
1351cb0ef41Sopenharmony_ci  },
1361cb0ef41Sopenharmony_ci  ...
1371cb0ef41Sopenharmony_ci```
1381cb0ef41Sopenharmony_ci
1391cb0ef41Sopenharmony_ci### Expecting a certain number of results
1401cb0ef41Sopenharmony_ci
1411cb0ef41Sopenharmony_ciOne common use of `npm query` is to make sure there is only one version of
1421cb0ef41Sopenharmony_cia certain dependency in your tree.  This is especially common for
1431cb0ef41Sopenharmony_ciecosystems like that rely on `typescript` where having state split
1441cb0ef41Sopenharmony_ciacross two different but identically-named packages causes bugs.  You
1451cb0ef41Sopenharmony_cican use the `--expect-results` or `--expect-result-count` in your setup
1461cb0ef41Sopenharmony_cito ensure that npm will exit with an exit code if your tree doesn't look
1471cb0ef41Sopenharmony_cilike you want it to.
1481cb0ef41Sopenharmony_ci
1491cb0ef41Sopenharmony_ci
1501cb0ef41Sopenharmony_ci```sh
1511cb0ef41Sopenharmony_ci$ npm query '#react' --expect-result-count=1
1521cb0ef41Sopenharmony_ci```
1531cb0ef41Sopenharmony_ci
1541cb0ef41Sopenharmony_ciPerhaps you want to quickly check if there are any production
1551cb0ef41Sopenharmony_cidependencies that could be updated:
1561cb0ef41Sopenharmony_ci
1571cb0ef41Sopenharmony_ci```sh
1581cb0ef41Sopenharmony_ci$ npm query ':root>:outdated(in-range).prod' --no-expect-results
1591cb0ef41Sopenharmony_ci```
1601cb0ef41Sopenharmony_ci
1611cb0ef41Sopenharmony_ci### Package lock only mode
1621cb0ef41Sopenharmony_ci
1631cb0ef41Sopenharmony_ciIf 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).
1641cb0ef41Sopenharmony_ci
1651cb0ef41Sopenharmony_ci### Configuration
1661cb0ef41Sopenharmony_ci
1671cb0ef41Sopenharmony_ci#### `global`
1681cb0ef41Sopenharmony_ci
1691cb0ef41Sopenharmony_ci* Default: false
1701cb0ef41Sopenharmony_ci* Type: Boolean
1711cb0ef41Sopenharmony_ci
1721cb0ef41Sopenharmony_ciOperates in "global" mode, so that packages are installed into the `prefix`
1731cb0ef41Sopenharmony_cifolder instead of the current working directory. See
1741cb0ef41Sopenharmony_ci[folders](/configuring-npm/folders) for more on the differences in behavior.
1751cb0ef41Sopenharmony_ci
1761cb0ef41Sopenharmony_ci* packages are installed into the `{prefix}/lib/node_modules` folder, instead
1771cb0ef41Sopenharmony_ci  of the current working directory.
1781cb0ef41Sopenharmony_ci* bin files are linked to `{prefix}/bin`
1791cb0ef41Sopenharmony_ci* man pages are linked to `{prefix}/share/man`
1801cb0ef41Sopenharmony_ci
1811cb0ef41Sopenharmony_ci
1821cb0ef41Sopenharmony_ci
1831cb0ef41Sopenharmony_ci#### `workspace`
1841cb0ef41Sopenharmony_ci
1851cb0ef41Sopenharmony_ci* Default:
1861cb0ef41Sopenharmony_ci* Type: String (can be set multiple times)
1871cb0ef41Sopenharmony_ci
1881cb0ef41Sopenharmony_ciEnable running a command in the context of the configured workspaces of the
1891cb0ef41Sopenharmony_cicurrent project while filtering by running only the workspaces defined by
1901cb0ef41Sopenharmony_cithis configuration option.
1911cb0ef41Sopenharmony_ci
1921cb0ef41Sopenharmony_ciValid values for the `workspace` config are either:
1931cb0ef41Sopenharmony_ci
1941cb0ef41Sopenharmony_ci* Workspace names
1951cb0ef41Sopenharmony_ci* Path to a workspace directory
1961cb0ef41Sopenharmony_ci* Path to a parent workspace directory (will result in selecting all
1971cb0ef41Sopenharmony_ci  workspaces within that folder)
1981cb0ef41Sopenharmony_ci
1991cb0ef41Sopenharmony_ciWhen set for the `npm init` command, this may be set to the folder of a
2001cb0ef41Sopenharmony_ciworkspace which does not yet exist, to create the folder and set it up as a
2011cb0ef41Sopenharmony_cibrand new workspace within the project.
2021cb0ef41Sopenharmony_ci
2031cb0ef41Sopenharmony_ciThis value is not exported to the environment for child processes.
2041cb0ef41Sopenharmony_ci
2051cb0ef41Sopenharmony_ci#### `workspaces`
2061cb0ef41Sopenharmony_ci
2071cb0ef41Sopenharmony_ci* Default: null
2081cb0ef41Sopenharmony_ci* Type: null or Boolean
2091cb0ef41Sopenharmony_ci
2101cb0ef41Sopenharmony_ciSet to true to run the command in the context of **all** configured
2111cb0ef41Sopenharmony_ciworkspaces.
2121cb0ef41Sopenharmony_ci
2131cb0ef41Sopenharmony_ciExplicitly setting this to false will cause commands like `install` to
2141cb0ef41Sopenharmony_ciignore workspaces altogether. When not set explicitly:
2151cb0ef41Sopenharmony_ci
2161cb0ef41Sopenharmony_ci- Commands that operate on the `node_modules` tree (install, update, etc.)
2171cb0ef41Sopenharmony_ciwill link workspaces into the `node_modules` folder. - Commands that do
2181cb0ef41Sopenharmony_ciother things (test, exec, publish, etc.) will operate on the root project,
2191cb0ef41Sopenharmony_ci_unless_ one or more workspaces are specified in the `workspace` config.
2201cb0ef41Sopenharmony_ci
2211cb0ef41Sopenharmony_ciThis value is not exported to the environment for child processes.
2221cb0ef41Sopenharmony_ci
2231cb0ef41Sopenharmony_ci#### `include-workspace-root`
2241cb0ef41Sopenharmony_ci
2251cb0ef41Sopenharmony_ci* Default: false
2261cb0ef41Sopenharmony_ci* Type: Boolean
2271cb0ef41Sopenharmony_ci
2281cb0ef41Sopenharmony_ciInclude the workspace root when workspaces are enabled for a command.
2291cb0ef41Sopenharmony_ci
2301cb0ef41Sopenharmony_ciWhen false, specifying individual workspaces via the `workspace` config, or
2311cb0ef41Sopenharmony_ciall workspaces via the `workspaces` flag, will cause npm to operate only on
2321cb0ef41Sopenharmony_cithe specified workspaces, and not on the root project.
2331cb0ef41Sopenharmony_ci
2341cb0ef41Sopenharmony_ciThis value is not exported to the environment for child processes.
2351cb0ef41Sopenharmony_ci
2361cb0ef41Sopenharmony_ci#### `package-lock-only`
2371cb0ef41Sopenharmony_ci
2381cb0ef41Sopenharmony_ci* Default: false
2391cb0ef41Sopenharmony_ci* Type: Boolean
2401cb0ef41Sopenharmony_ci
2411cb0ef41Sopenharmony_ciIf set to true, the current operation will only use the `package-lock.json`,
2421cb0ef41Sopenharmony_ciignoring `node_modules`.
2431cb0ef41Sopenharmony_ci
2441cb0ef41Sopenharmony_ciFor `update` this means only the `package-lock.json` will be updated,
2451cb0ef41Sopenharmony_ciinstead of checking `node_modules` and downloading dependencies.
2461cb0ef41Sopenharmony_ci
2471cb0ef41Sopenharmony_ciFor `list` this means the output will be based on the tree described by the
2481cb0ef41Sopenharmony_ci`package-lock.json`, rather than the contents of `node_modules`.
2491cb0ef41Sopenharmony_ci
2501cb0ef41Sopenharmony_ci
2511cb0ef41Sopenharmony_ci
2521cb0ef41Sopenharmony_ci#### `expect-results`
2531cb0ef41Sopenharmony_ci
2541cb0ef41Sopenharmony_ci* Default: null
2551cb0ef41Sopenharmony_ci* Type: null or Boolean
2561cb0ef41Sopenharmony_ci
2571cb0ef41Sopenharmony_ciTells npm whether or not to expect results from the command. Can be either
2581cb0ef41Sopenharmony_citrue (expect some results) or false (expect no results).
2591cb0ef41Sopenharmony_ci
2601cb0ef41Sopenharmony_ciThis config can not be used with: `expect-result-count`
2611cb0ef41Sopenharmony_ci
2621cb0ef41Sopenharmony_ci#### `expect-result-count`
2631cb0ef41Sopenharmony_ci
2641cb0ef41Sopenharmony_ci* Default: null
2651cb0ef41Sopenharmony_ci* Type: null or Number
2661cb0ef41Sopenharmony_ci
2671cb0ef41Sopenharmony_ciTells to expect a specific number of results from the command.
2681cb0ef41Sopenharmony_ci
2691cb0ef41Sopenharmony_ciThis config can not be used with: `expect-results`
2701cb0ef41Sopenharmony_ci## See Also
2711cb0ef41Sopenharmony_ci
2721cb0ef41Sopenharmony_ci* [dependency selectors](/using-npm/dependency-selectors)
273