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.
_10npm 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 inpackage-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_11npm ls lodash_11_11# Only your direct dependencies_11npm ls --depth=0_11_11# Every package, including nested ones_11npm ls --all_11_11# Globally installed packages_11npm 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.
_10npm 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_10npm view react version_10_10# Every published version_10npm view react versions_10_10# Release tags such as latest, next or canary_10npm view react dist-tags
Compare everything with npm outdated
npm outdated lists every dependency that is behind, with three columns:
_10npm 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.jsonrange allows.npm updatemoves 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:
_10import { createRequire } from 'node:module';_10_10const require = createRequire(import.meta.url);_10console.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.0might mean 1.9.3 is installed. Check withnpm ls. - Running in the wrong folder.
npm lsreports 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.
npxprefers the local one. Check both withnpm ls nameandnpm ls -g name. - Assuming one copy. Two copies of a library like React in one app cause strange errors. Look for duplicates in
npm lsoutput. - Using pnpm or Yarn. Their equivalents are
pnpm listandyarn why. The npm commands read npm's own lockfile.
Related reading
#Was this resource helpful?