SELECTORS(7)
πΈ NAME πΈ
SELECTORS(7) β Selectors - Dependency Selector Syntax & Querying π¦
okay bestie, this is the whole vibe of dependency selectors in npm. no cap, it's lowkey iconic once the grammar clicks. π
π Description π
so the npm help query command exposes a whole new dependency selector syntax β it's informed by & respects lots of the CSS Selectors 4 Spec (https://dev.w3.org/csswg/selectors4/#relational) β and it:
- Standardizes the shape of, & querying of, dependency graphs with a robust object model, metadata & selector syntax β¨
- Leverages existing, known language syntax & operators from CSS to make disparate package information broadly accessible π
- Unlocks the ability to answer complex, multi-faceted questions about dependencies, their relationships & associative metadata π₯
- Consolidates redundant logic of similar query commands in npm (ex.
npm fund,npm ls,npm outdated,npm audit...) π
π§ Dependency Selector Syntax π§
Overview: π
- there is no "type" or "tag" selectors (ex.
div,h1,a) as a dependency/target is the only type of Node that can be queried - the term "dependencies" is in reference to any Node found in a tree returned by Arborist π³
Combinators
>β direct descendant/child(space) β any descendant/child~β sibling
Selectors
*β universal selector#<name>β dependency selector (equivalent to[name="..."])#<name>@<version>β (equivalent to[name=<name>]:semver(<version>)),β selector list delimiter.β dependency type selector:β pseudo selector
Dependency Type Selectors
.prodβ dependency found in thedependenciessection ofpackage.json, or is a child of said dependency.devβ dependency found in thedevDependenciessection ofpackage.json, or is a child of said dependency.optionalβ dependency found in theoptionalDependenciessection ofpackage.json, or has"optional": trueset in its entry in thepeerDependenciesMetasection ofpackage.json, or a child of said dependency.peerβ dependency found in thepeerDependenciessection ofpackage.json.workspaceβ dependency found in theworkspaces(https://docs.npmjs.com/cli/v8/using-npm/workspaces) section ofpackage.json.bundledβ dependency found in thebundleDependenciessection ofpackage.json, or is a child of said dependency
Pseudo Selectors
:not(<selector>)β the CSS:not()situation (https://developer.mozilla.org/en-US/docs/Web/CSS/:not):has(<selector>)β the CSS:has()situation (https://developer.mozilla.org/en-US/docs/Web/CSS/:has):is(<selector list>)β the CSS:is()situation (https://developer.mozilla.org/en-US/docs/Web/CSS/:is):rootβ matches the root node/dependency (https://developer.mozilla.org/en-US/docs/Web/CSS/:root):scopeβ matches node/dependency it was queried against (https://developer.mozilla.org/en-US/docs/Web/CSS/:scope):emptyβ when a dependency has no dependencies (https://developer.mozilla.org/en-US/docs/Web/CSS/:empty):privateβ when a dependency is private (https://docs.npmjs.com/cli/v8/configuring-npm/package-json#private):linkβ when a dependency is linked (for instance, workspaces or packages manually linked (https://docs.npmjs.com/cli/v8/commands/npm-link)):dedupedβ when a dependency has been deduped (note that this does not always mean the dependency has been hoisted to the root ofnode_modules):overriddenβ when a dependency has been overridden:extraneousβ when a dependency exists but is not defined as a dependency of any node:invalidβ when a dependency version is out of its ancestors specified range:missingβ when a dependency is not found on disk:semver(<spec>, [selector], [function])β match a validnode-semver(https://github.com/npm/node-semver) version or range to a selector:path(<path>)β glob (https://www.npmjs.com/package/glob) matching based on dependencies path relative to the project:type(<type>)β based on currently recognized types (https://github.com/npm/npm-package-arg#result-object). You can also use the aggregate type ofregistryfor any registry dependency (e.g.tag,version,range,alias):outdated(<type>)β when a dependency is outdated:vuln(<selector>)β when a dependency has a known vulnerability
π§ͺ :semver(<spec>, [selector], [function]) π§ͺ
the :semver() pseudo selector lets you compare fields from each node's package.json using semver (https://github.com/npm/node-semver#readme) methods. It accepts up to 3 parameters, all but the first of which are optional. π
specβ a semver version or rangeselectorβ an attribute selector for each node (default[version])functionβ a semver method to apply, one of:satisfies,intersects,subset,gt,gte,gtr,lt,lte,ltr,eq,neqor the special functioninfer(defaultinfer)
When the special infer function is used the spec and the actual value from the node are compared. If both are versions, according to semver.valid(), eq is used. If both values are ranges, according to !semver.valid(), intersects is used. If the values are mixed types satisfies is used.
Some examples:
:semver(^1.0.0)returns every node that has a version satisfied by the provided range^1.0.0:semver(16.0.0, :attr(engines, [node]))returns every node which has anengines.nodeproperty satisfying the version16.0.0:semver(1.0.0, [version], lt)every node with a version less than1.0.0
β° :outdated(<type>) β°
the :outdated pseudo selector retrieves data from the registry and returns information about which of your dependencies are outdated. The type parameter may be one of the following:
any(default) β a version exists that is greater than the current onein-rangeβ a version exists that is greater than the current one, and satisfies at least one if its parent's dependenciesout-of-rangeβ a version exists that is greater than the current one, does not satisfy at least one of its parent's dependenciesmajorβ a version exists that is a semver major greater than the current oneminorβ a version exists that is a semver minor greater than the current onepatchβ a version exists that is a semver patch greater than the current one
In addition to the filtering performed by the pseudo selector, some extra data is added to the resulting objects. The following data can be found under the queryContext property of each node.
versionsβ an array of every available version of the given nodeoutdated.inRangeβ an array of objects, each with afromandversions, wherefromis the on-disk location of the node that depends on the current node andversionsis an array of all available versions that satisfies that dependency. This is only populated if:outdated(in-range)is used.outdated.outOfRangeβ an array of objects, identical in shape toinRange, but where theversionsarray is every available version that does not satisfy the dependency. This is only populated if:outdated(out-of-range)is used.
Some examples:
:root > :outdated(major)returns every direct dependency that has a new semver major release.prod:outdated(in-range)returns production dependencies that have a new release that satisfies at least one of its parent's dependencies
π‘οΈ :vuln π‘οΈ
the :vuln pseudo selector retrieves data from the registry and returns information about which of your dependencies has a known vulnerability. Only dependencies whose current version matches a vulnerability will be returned. For example if you have semver@7.6.0 in your tree, a vulnerability for semver which affects versions <=6.3.1 will not match.
You can also filter results by certain attributes in advisories. Currently that includes severity and cwe. Note that severity filtering is done per severity, it does not include severities "higher" or "lower" than the one specified.
In addition to the filtering performed by the pseudo selector, info about each relevant advisory will be added to the queryContext attribute of each node under the advisories attribute.
Some examples:
:root > .prod:vulnreturns direct production dependencies with any known vulnerability:vuln([severity=high])returns only dependencies with a vulnerability with a high severity.:vuln([severity=high],[severity=moderate])returns only dependencies with a vulnerability with a high or moderate severity.:vuln([cwe=1333])returns only dependencies with a vulnerability that includes CWE-1333 (ReDoS)
π Attribute Selectors π
CSS attribute selector energy (https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors) β the attribute selector evaluates the key/value pairs in package.json if they are Strings.
[]β attribute selector (ie. existence of attribute)[attribute=value]β attribute value is equivalent...[attribute~=value]β attribute value contains word...[attribute*=value]β attribute value contains string...[attribute|=value]β attribute value is equal to or starts with...[attribute^=value]β attribute value starts with...[attribute$=value]β attribute value ends with...
π§Ί Array & Object Attribute Selectors π§Ί
the generic :attr() pseudo selector standardizes a pattern which can be used for attribute selection of Objects, Arrays or Arrays of Objects accessible via Arborist's Node.package metadata. This allows for iterative attribute selection beyond top-level String evaluation. The last argument passed to :attr() must be an attribute selector or a nested :attr(). See examples below:
Objects
/* return dependencies that have a `scripts.test` containing `"tap"` */
*:attr(scripts, [test~=tap])
Nested Objects
Nested objects are expressed as sequential arguments to :attr().
/* return dependencies that have a [testling config](https://ci.testling.com/guide/advanced_configuration) for opera browsers */
*:attr(testling, browsers, [~=opera])
Arrays
Arrays specifically uses a special/reserved . character in place of a typical attribute name. Arrays also support exact value matching when a String is passed to the selector.
Example of an Array Attribute Selection:
/* removes the distinction between properties & arrays */
/* ie. we'd have to check the property & iterate to match selection */
*:attr([keywords^=react])
*:attr(contributors, :attr([name~=Jordan]))
Example of an Array matching directly to a value: this one returns dependencies that have the exact keyword "react", and itβs equivalent to *:keywords([value="react"]):
/* return dependencies that have the exact keyword "react" */
/* this is equivalent to `*:keywords([value="react"])` */
*:attr([keywords=react])
Example of an Array of Objects: this is the contributor-by-email moment:
/* returns */
*:attr(contributors, [email=ruyadorno@github.com])
πΈ Groups πΈ
Dependency groups are defined by the package relationships to their ancestors (ie. the dependency types that are defined in package.json). This approach is user-centric, because the ecosystem has been taught to think about dependencies in these groups first-and-foremost. Dependencies are allowed to be included in multiple groups (ex. a prod dependency may also be a dev dependency (in that itβs also required by another dev dependency) & may also be bundled β a selector for that type of dependency would look like: *.prod.dev.bundled).
.prod.dev.optional.peer.bundled.workspace
Please note: currently workspace deps are always prod dependencies. Additionally the .root dependency is also considered a prod dependency.
π» Programmatic Usage π»
- Arboristβs
NodeClass has a.querySelectorAll()method- this method will return a filtered, flattened dependency Arborist Node list based on a valid query selector
const Arborist = require('@npmcli/arborist')
const arb = new Arborist({})
// root-level
arb.loadActual().then(async (tree) => {
// query all production dependencies
const results = await tree.querySelectorAll('.prod')
console.log(results)
})
// iterative
arb.loadActual().then(async (tree) => {
// query for the deduped version of react
const results = await tree.querySelectorAll('#react:not(:deduped)')
// query the deduped react for git deps
const deps = await results[0].querySelectorAll(':type(git)')
console.log(deps)
})
π SEE ALSO π
npm help queryβ the query commandβs help page, go get your read on π@npmcli/arboristβ¨https://npm.im/@npmcli/arboristβ© β the package itself, iconic
and thatβs the vibe check, bestie π«Ά the footer energy for this one is NPM@11.17.0, June 2026, SELECTORS(7) β screenshot it and slay β¨π
π¦ where this page comes from
- centos-stream 10: nodejs24-npm 1:11.17.0-1.24.19.0.1.el10 Β· Apache-2.0 AND Artistic-2.0 AND BSD-2-Clause AND BSD-3-Clause AND BlueOak-1.0.0 AND CC-BY-3.0 AND CC0-1.0 AND ISC AND MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms