vlt / docs

  • PricingBenchmarks (opens in new window)Community (opens in new window)Feedback
  • Overview
  • Attribute Selectors
  • Combinators
  • ID Selectors
  • Security Insights
    • Overview
    • :attr()
    • :has()
    • :dist()
    • :host()
    • :is()
    • :not()
    • :outdated()
    • :published()
    • :semver()
    • :spec()
    • :path()
    • :type()
    • :diff()
    • :hostname()
    • :registry()
  • Syntax
  • Examples
  • Optional peer dependencies
  • Engine requirements
  • Scripts
  • What gets selected
  • Combining with combinators
  • See also
  1. Client
  2. /
  3. Selectors
  4. /
  5. Pseudo-classes
  6. /
  7. :attr()

:attr()

The :attr() pseudo-class selects packages based on nested properties of their package.json metadata. While attribute selectors match top-level fields, :attr() lets you drill into nested objects.

Syntax

Text
:attr(key, [attr=value])
:attr(key, nestedKey, [attr=value])

Arguments are a path of property keys, ending with a standard attribute selector.

Examples

Optional peer dependencies

Select packages that declare foo as an optional peer dependency:

Terminal
$ vlt query ':attr(peerDependenciesMeta, foo, [optional=true])'

This is equivalent to checking:

JSON
{
  "peerDependenciesMeta": {
    "foo": {
      "optional": true
    }
  }
}

Engine requirements

Select packages that have a node engine requirement:

Terminal
$ vlt query ':attr(engines, [node])'

Scripts

Select packages that have a build script:

Terminal
$ vlt query ':attr(scripts, [build])'

Select packages that have a test script using vitest:

Terminal
$ vlt query ':attr(scripts, [test*=vitest])'

What gets selected

Given this dependency graph:

Text
my-app
├── react@18.2.0
│   └── loose-envify@1.4.0  ← has scripts.postinstall
├── typescript@5.3.0         ← has engines.node
└── vite@5.0.0               ← has engines.node

The query :attr(engines, [node]) selects:

Text
my-app
├── react@18.2.0
│   └── loose-envify@1.4.0
├── typescript@5.3.0          ✅ has engines.node
└── vite@5.0.0                ✅ has engines.node

Combining with combinators

Find direct dependencies that have a build script:

Terminal
$ vlt query ':project > :attr(scripts, [build])'

See also

  • Attribute Selectors — top-level property matching
  • :semver() — compare semver values in nested properties

PreviousOverviewNext:has()

On this page

  • Syntax
  • Examples
  • Optional peer dependencies
  • Engine requirements
  • Scripts
  • What gets selected
  • Combining with combinators
  • See also
Edit this page

Deploy your package on vlt.io

Publish scoped and private packages, manage organizations and access, and give every developer and CI environment a consistent source for public and private JavaScript dependencies.

Publish now