Start Using the npm Query Today: Powerful Commands for Every Developer
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (179).NET Core  (29).NET MAUI  (214)Angular  (110)ASP.NET  (51)ASP.NET Core  (83)ASP.NET MVC  (90)Azure  (41)Black Friday Deal  (1)Blazor  (226)BoldSign  (15)DocIO  (24)Essential JS 2  (109)Essential Studio  (200)File Formats  (68)Flutter  (133)JavaScript  (226)Microsoft  (120)PDF  (81)Python  (1)React  (105)Streamlit  (1)Succinctly series  (131)Syncfusion  (942)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (52)Windows Forms  (61)WinUI  (71)WPF  (163)Xamarin  (161)XlsIO  (38)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (9)Business intelligence  (55)Button  (4)C#  (157)Chart  (140)Chart of the week  (54)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (70)Development  (658)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (7)Essential Tools  (14)Excel  (43)Extensions  (22)File Manager  (7)Gantt  (19)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (512)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (54)Security  (4)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (14)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (393)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (614)What's new  (333)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Start Using the npm Query Today: Powerful Commands for Every Developer

Start Using the npm Query Today: Powerful Commands for Every Developer

TL;DR: The npm query is a new, top-level command that takes a dependency selector and provides a filtered list of dependencies from your project. This blog shows common and advanced use cases of the npm query with code.

Dependency management is a core concept in Node.js development due to the enormous amount of dependencies that must be maintained. Many of the common difficulties developers experience with node modules are caused by not knowing how node package manager’s (npm) version management and package resolution work. To understand your project dependencies and metadata better and how they help you manage and develop your apps, use the npm query.

In this article, I will briefly introduce the npm query and its usage in software development.

What is the npm query?

Did you know that you can use a specific query to identify packages in the node_modules directory that match it? Since npm version 8.18.0, we have been able to accomplish that using the npm query.

The npm query is a new, top-level command that takes a dependency selector and provides a filtered list of dependencies from your project. This feature has been missing from package managers for a long time. With the npm query’s release, developers can query their dependencies, connections, and correlated metadata.

Usage of the npm query

But what is the npm query for?

After its first release, the software is continually updated and maintained. In the long run, you should know if packages have duplicates or what packages utilize postinstall scripts. Instead of having to check all the dependencies manually, the npm query makes it simple to find particular packages.

The npm query helps us see possible problems caused by packages with shared dependencies on outdated or incompatible versions. Also, it’s beneficial in resolving unanticipated problems caused by failing to resolve duplicate or conflicting dependencies and unintentionally upgrading a package that makes a breaking change.

For instance, you can look for restrictions in your dependencies to ensure that you don’t have duplicate dependencies or licenses that conflict with your app or organization policies.

Example uses of the npm query

To list all the dependencies of your project, Similar to npm list –all.

Refer to the following command.

npm query "*"

To list all the dependencies named react or all the versions of react in your project.

npm query "#react"

Following is the response to this command. The response is an array of dependency objects that may include different copies of the same package.

    "name": "react",
    "description": "React is a JavaScript library for building user interfaces.",
    "keywords": [
    "version": "17.0.2",
    "homepage": "",
    "bugs": "",
    "license": "MIT",
    "files": [
    "main": "index.js",
    "repository": {
      "type": "git",
      "url": "",
      "directory": "packages/react"
    "engines": {
      "node": ">=0.10.0"
    "dependencies": {
      "loose-envify": "^1.1.0",
      "object-assign": "^4.1.1"
    "browserify": {
      "transform": [
    "_id": "react@17.0.2",
    "pkgid": "react@17.0.2",
    "location": "node_modules/react",
    "path": "D:\\Development\\Stampen Media\\react_frontend\\services\\frontend\\node_modules\\react",
    "realpath": "D:\\Development\\Stampen Media\\react_frontend\\services\\frontend\\node_modules\\react",
    "resolved": "",
    "from": [
    "to": [
    "dev": false,
    "inBundle": false,
    "deduped": false,
    "overridden": false

Here are some of the most-used npm query selectors:

  • * Universal Selector
  • #<name> dependency selector by name
  • #<name>@<version> dependency selector by name and version
  • , selector list delimiter
  • . dependency type selector
  • : pseudo selector

You can list all the root dev dependencies with the following command.

npm query ":root > .dev"

Here, we are using the > combinator. Following are the combinators in the npm query:

  • > direct descendant/child
  • [space] any descendant/child
  • ~ sibling

In the previous example, you can see a dependency type selector. Its syntax starts with .(dot), and other dependency types are:

  • .prod
  • .optional
  • .peer
  • .workspace
  • .bundled

You can use the pseudo selector to find all the dependencies installed from Git branches or tagsThe syntax starts with a colon (:).

npm query ":type(git)"

There are many other pseudo-selectors such as :not, :has, :is, and :root.

You can search for packages that have particular licenses or don’t have any licenses. You can also use the attribute selector, which evaluates the key/value pairs or existence of attributes in the package.json.

npm query "[license=MIT], [license=ISC], [license=]"

Some advance use cases of the npm query

Let’s see how to find the dependencies that make use of postinstall scripts.

npm query ":attr(scripts, [postinstall])"

You can use the jq program to filter data in JSON format. As an example, if you want to get only the dependency names from the previous command.

npm query ":attr(scripts, [postinstall])" | jq 'map(.name)'

The response will be as follows.


If you want to examine the dependency list and uninstall those items with a postinstall script, you can do it with the following command.

npm query ":attr(scripts, [postinstall])" | jq 'map(.name) | join("\n")' -r | xargs -I {} npm uninstall {}

With all these examples, you can see that the npm query has a unique method for selecting dependencies. Let’s look at that in detail in the following section.

Dependency selector

The npm query command exposes a new dependency selector syntax that adapts CSS selectors. The dependency selector:

  • Standardizes dependency graph shape and querying with a robust object model, metadata, and selector syntax.
  • Makes use of CSS’s established, well-known language syntax and operators to make package information widely accessible.
  • Opens the door to being able to respond to intricate, multifaceted queries about dependencies, their connections, and related metadata.
  • Consolidates the redundant logic of npm’s fund, ls, and other similar query commands.

Dependency Selector Syntax v1.0.0: The dependency selector syntax introduces various concepts and functionalities to enhance package management. Refer to the official documentation for comprehensive information about its syntax and usage.

Programmatic usage

Behind the scenes, the npm CLI uses an Arborist to inspect and manage the node_modules tree. Based on a valid query selector, the .querySelectorAll() method of the Arborist’s Node class delivers a filtered, flattened list of dependency Arborist nodes.

const Arborist = require('@npmcli/arborist')
const arb = new Arborist({})

arb.loadActual().then(async (tree) => {
  // query all production dependencies.
  const results = await tree.querySelectorAll('.prod')

StackBlitz reference

For more details, refer to the working example in Stackblitz. You can run the example with the commands npm install and npm run.


Thank you for reading this blog, which explores how developers can independently navigate and comprehend project dependencies, thereby enhancing project management and development efficiency using the npm query.

I encourage you to integrate npm query into your projects to optimize dependency management with npm. Share your experiences with the npm query in the comments section below to contribute to the collective knowledge of the developer community.

If you have any questions or comments, you can contact us through our support forums, support portal, or feedback portal. We are always happy to assist you!

Related blogs


Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed