More Node.js answers

by Danny
Published 2 min read

Run npm ls with the package name to see the installed version, npm view to see the latest published one, and npm outdated to compare everything in your project at once.

The short answer

#

Run npm ls with the package name from your project folder. It shows the version actually installed in node_modules, including any nested copies.


_10
npm ls react
_10
# [email protected] /Users/me/my-app
_10
# └── [email protected]

Use npm view react version to see the latest version on the registry, and npm outdated to compare everything at once.

Why it happens

#

There are three different version numbers you might mean, and they are often not the same:

  • The range in package.json, such as ^19.0.0. This is what you allow, not what you have.
  • The installed version in node_modules, which is recorded in package-lock.json. This is what your code actually runs.
  • The latest published version on the npm registry.

On top of that, the same package can be installed more than once. If your app needs one version and a dependency needs another, npm installs both in different places in the tree. Looking only at package.json hides all of this, which is why the tools below are more reliable.

How to fix it

#

See the installed version with npm ls


_11
# One package, every copy in the tree
_11
npm ls lodash
_11
_11
# Only your direct dependencies
_11
npm ls --depth=0
_11
_11
# Every package, including nested ones
_11
npm ls --all
_11
_11
# Globally installed packages
_11
npm ls -g --depth=0

If a package appears more than once with different versions, npm ls shows each copy under the package that needs it. Add --json if you want to read the output from a script.

Find out why a version is installed

When you see a version you did not expect, npm explain (also available as npm why) shows the chain of packages that pulled it in.


_10
npm explain lodash

This is useful when a security audit flags an old version deep in the tree.

See published versions with npm view

npm view asks the registry, so it works even if the package is not installed.


_10
# Latest version
_10
npm view react version
_10
_10
# Every published version
_10
npm view react versions
_10
_10
# Release tags such as latest, next or canary
_10
npm view react dist-tags

Compare everything with npm outdated

npm outdated lists every dependency that is behind, with three columns:


_10
npm outdated
_10
# Package Current Wanted Latest
_10
# vitest 3.1.0 3.2.4 4.0.8

  • Current is what is installed.
  • Wanted is the newest version your package.json range allows. npm update moves you here.
  • Latest is the newest version published. Getting there may need npm install name@latest, and it may include breaking changes.

The numbers above are only an example; you will see your own.

Check the version from code

Sometimes you need the version at runtime, for example in a bug report. Many packages let you import their package.json:


_10
import { createRequire } from 'node:module';
_10
_10
const require = createRequire(import.meta.url);
_10
console.log(require('react/package.json').version);

This fails with ERR_PACKAGE_PATH_NOT_EXPORTED if the package uses an exports field that does not include package.json. In that case, use npm ls instead, or read your own lockfile.

Common pitfalls

#
  • Trusting package.json. ^1.2.0 might mean 1.9.3 is installed. Check with npm ls.
  • Running in the wrong folder. npm ls reports the project in the current directory. In a monorepo, run it in the workspace you care about, or use --workspaces.
  • Confusing local and global. A command line tool might be installed globally at one version and locally at another. npx prefers the local one. Check both with npm ls name and npm ls -g name.
  • Assuming one copy. Two copies of a library like React in one app cause strange errors. Look for duplicates in npm ls output.
  • Using pnpm or Yarn. Their equivalents are pnpm list and yarn why. The npm commands read npm's own lockfile.
#

Was this resource helpful?