Skip to content

The :semver() pseudo-class matches packages based on semver comparisons. By default it compares against the package version field, but it can be configured to compare any semver-like value. It also supports range-vs-range functions (subset, intersects) that operate on edges by comparing dependency specifier ranges.

Syntax

:semver(<range>)
:semver(<range>, <function>)
:semver(<range>, <function>, <custom-attribute-selector>)

Parameters

ParameterDescriptionDefault
rangeThe semver range to compare against(required)
functionComparison functionsatisfies
custom-attribute-selectorA different property to compareversion

Comparison functions

FunctionDescriptionOperates on
satisfiesVersion satisfies the range (default)nodes
eqVersion exactly equals the rangenodes
neqVersion does not equal the rangenodes
gtVersion is greater than the rangenodes
gteVersion is greater than or equal to the rangenodes
ltVersion is less than the rangenodes
lteVersion is less than or equal to the rangenodes
intersectsEdge’s range intersects with the provided rangeedges
subsetEdge’s range is a subset of the provided rangeedges

Examples

Basic range matching

Find packages satisfying ^1.0.0:

Terminal
$ vlt query ':semver(^1.0.0)'

Exact version matching

Find packages at exactly version 2.0.0:

Terminal
$ vlt query ':semver(2.0.0, eq)'

Version comparison

Find packages greater than version 5:

Terminal
$ vlt query ':semver(5.0.0, gt)'

Range-vs-range comparison

The intersects and subset functions compare two ranges rather than a version against a range. By default they operate on edges using the dependency specifier (bareSpec), making it possible to query which declared dependency ranges relate to a given range.

Find edges whose declared range is a subset of >=2:

Terminal
$ vlt query ':semver(>=2, subset)'

Find edges whose declared range intersects ^3:

Terminal
$ vlt query ':semver(^3, intersects)'

When a custom attribute is provided, intersects and subset compare against that manifest property value as a range:

Terminal
$ vlt query ':semver(>=20, subset, :attr(engines, [node]))'

Custom property comparison

Match packages whose engines.node field satisfies ^22:

Terminal
$ vlt query ':semver(^22, satisfies, :attr(engines, [node]))'

What gets selected

Given:

my-app
├── react@18.2.0
├── react-dom@18.2.0
├── typescript@5.3.0
└── lodash@4.17.21
QuerySelected
:semver(^18.0.0)react, react-dom
:semver(5.0.0, gte)typescript
:semver(^4.0.0)lodash

Combining with other selectors

Find direct dependencies with version >= 18:

Terminal
$ vlt query ':root > :semver(18.0.0, gte)'

See also