TExcelTAD
Home › JS Packages & Bundlers: npm, Yarn, pnpm, Webpack, Rollup, esbuild › npm ls, explain, outdated, query & view

npm ls, explain, outdated, query & view

A real project's tree has hundreds of packages you never chose. When something breaks ("why is there a second copy of React?", "who pulls in this deprecated package?", "what would an update change?"), these commands answer the question without opening node_modules.

Question Command
What is installed, as a tree? npm ls (--all for every level, --depth=N)
Why is package X here? npm explain x (alias npm why)
What could be updated? npm outdated
Which installed packages match a condition? npm query '<selector>'
What does the registry know about X? npm view x [field]

All of them work offline against the local registry; npm audit doesn't, because it needs the public advisory database.

A project to inspect

Every session in this lesson starts from the same small project: webpack-cli's tree is large enough to be interesting.

bash · try it yourself
mkdir -p shop && cd shop
npm init -y > /dev/null
npm install chalk@4.1.2 lodash@4.17.21 is-odd@3.0.1 > /dev/null
npm install -D webpack@5.112.2 webpack-cli@7.2.3 > /dev/null 2>&1
npm ls
echo "--- prod only, one level"
npm ls --omit=dev --depth=1
npm ls --all | wc -l
shop@1.0.0 /home/learner/shop
├── chalk@4.1.2
├── is-odd@3.0.1
├── lodash@4.17.21
├── webpack-cli@7.2.3
└── webpack@5.112.2

--- prod only, one level
shop@1.0.0 /home/learner/shop
├─┬ chalk@4.1.2
│ ├── ansi-styles@4.3.0
│ └── supports-color@7.2.0
├─┬ is-odd@3.0.1
│ └── is-number@6.0.0
└── lodash@4.17.21

154

Why is it here?

npm explain walks the tree upwards from a package to your package.json, listing every path:

bash · try it yourself
mkdir -p shop && cd shop
npm init -y > /dev/null
npm install chalk@4.1.2 > /dev/null
npm install -D webpack@5.112.2 webpack-cli@7.2.3 > /dev/null 2>&1
npm explain supports-color
supports-color@8.1.1 dev
node_modules/jest-worker/node_modules/supports-color
  supports-color@"^8.0.0" from jest-worker@27.5.1
  node_modules/jest-worker
    jest-worker@"^27.4.5" from minimizer-webpack-plugin@5.13.1
    node_modules/minimizer-webpack-plugin
      minimizer-webpack-plugin@"^5.13.0" from webpack@5.112.2
      node_modules/webpack
        dev webpack@"^5.112.2" from the root project
        peer webpack@"^5.1.0" from minimizer-webpack-plugin@5.13.1
        peerOptional webpack@"^5.112.0" from schema-utils@4.8.1
        node_modules/schema-utils
          schema-utils@"^4.6.0" from minimizer-webpack-plugin@5.13.1
          schema-utils@"^4.8.1" from webpack@5.112.2
        peer webpack@"^5.101.0" from webpack-cli@7.2.3
        node_modules/webpack-cli
          dev webpack-cli@"^7.2.3" from the root project

supports-color@7.2.0
node_modules/supports-color
  supports-color@"^7.1.0" from chalk@4.1.2
  node_modules/chalk
    chalk@"^4.1.2" from the root project

Two versions of supports-color: 7.2.0 for chalk at the top, 8.1.1 nested under jest-worker, deep inside webpack's minifier. The labels tell you how each edge was declared: dev, peer, peerOptional. Each npm explain block shows the version, its folder (nested or top-level) and the chain of dependents.

What's outdated?

bash · try it yourself
mkdir -p shop && cd shop
npm init -y > /dev/null
npm install chalk@4.1.2 lodash@4.17.21 semver@6.3.1 ms@2.1.3 > /dev/null
npm outdated; echo "exit code: $?"
Package  Current  Wanted  Latest  Location             Depended by
chalk      4.1.2   4.1.2   6.0.1  node_modules/chalk   shop
lodash   4.17.21  4.18.1  4.18.1  node_modules/lodash  shop
semver     6.3.1   6.3.1   7.8.5  node_modules/semver  shop
exit code: 1
Column Meaning
Current installed now
Wanted the highest version your range allows; npm update goes here
Latest the registry's latest dist-tag; reaching it may need a major bump in package.json

lodash can move with npm update. chalk and semver need npm install chalk@6 and a look at their changelogs: chalk 5+ is ESM-only, a classic breaking change. The exit code is 1 when anything is outdated, so CI can report it.

Querying the tree with selectors

npm query takes CSS-like selectors over the installed tree and prints JSON, which jq turns into answers:

bash · try it yourself
mkdir -p shop && cd shop
npm init -y > /dev/null
npm install chalk@4.1.2 lodash@4.17.21 > /dev/null
npm install -D webpack@5.112.2 > /dev/null 2>&1
echo "direct prod deps:";   npm query ':root > .prod' | jq -r '.[].name'
echo "license counts:";     npm query '*' | jq -r '.[].license' | sort | uniq -c | sort -rn | head -4
echo "dev-only packages:";   npm query '.dev' | jq length
echo "duplicated names:";   npm query '*' | jq -r '.[].name' | sort | uniq -d
direct prod deps:
chalk
lodash
license counts:
     58 MIT
      4 ISC
      3 BSD-3-Clause
      3 Apache-2.0
dev-only packages:
63
duplicated names:
supports-color

That's an inventory in four lines: direct dependencies, licences, how much of the tree only exists for building, and duplicated names. Selectors include .prod, .dev, .peer, :root > *, [name^=@types], :outdated, :attr(...) and more (see npm help query in a normal install).

Asking the registry

npm view reads package metadata from the registry, without installing anything:

bash · try it yourself
npm view react versions
npm view react dist-tags
npm view react-dom@18.3.1 peerDependencies
npm view webpack-cli@7.2.3 engines bin
npm view lodash@4.17.21 dist.integrity
[ '18.3.1', '19.3.1' ]
{ latest: '19.3.1' }
{ react: '^18.3.1' }
engines = { node: '>=20.9.0' }
bin = { 'webpack-cli': 'bin/cli.js' }
sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==

Checking peerDependencies and engines before upgrading saves a failed install: react-dom 18.3.1 wants react@^18.3.1, and webpack-cli 7 needs Node 20.9 or newer.

Test yourself

In npm outdated, what is the Wanted column?

  1. The latest version on the registry
  2. The highest version that satisfies the range in package.json
  3. The version in the lockfile
  4. The version a peer wants

Which command shows every chain of dependents that pulls in a package?

  1. npm view
  2. npm explain (npm why)
  3. npm outdated
  4. npm pkg get

Why does npm audit fail in this course's sessions while npm outdated works?

  1. audit needs root
  2. audit needs the public advisory database; outdated only needs the configured registry
  3. audit is removed in npm 11
  4. outdated reads the lockfile only