Find the installed version of an npm package

Distinguish installed packages, manifest ranges, lockfile resolutions, and registry versions before diagnosing a dependency problem.

Best practices
Illustration of files, modules, and dependencies

Run npm ls package-name in the project to inspect the installed dependency tree. A registry query such as npm view package-name version answers a different question: which version the registry reports for the selected package specification.

Useful commands

# Find installed instances of a named dependency.
npm ls lodash

# List direct project dependencies.
npm ls --depth=0

# List global packages for the active npm installation.
npm ls --depth=0 --global

# Query the configured registry's default tag, normally latest.
npm view lodash version

# Query all published versions visible through that registry.
npm view lodash versions

npm list is an alias for npm ls. A package can appear at multiple versions in the tree. Run the command in the correct project or workspace, and check the active npm installation when looking at global packages.

Know what each file tells you

package.json declares the requested dependency specification, often a range such as ^4.17.21. It is not an inventory of what is installed now.

package-lock.json records a resolved dependency tree. Modern lockfile formats contain a packages map; older formats differ. A lockfile can disagree with the current filesystem if the installation is incomplete or has changed, so do not equate a locked version with proof that those files are present.

For a package that allows access to its package metadata, CommonJS can read:

const { version } = require('lodash/package.json');
console.log(version);

This resolves the package from the calling module's location. Some packages restrict subpaths through exports, so the same pattern is not universally available. Use a documented package API or npm's tree inspection when metadata is not exported.

FAQ

What does extraneous mean? npm found a package that is not required by the declared dependency tree. Inspect why it is there before removing it. npm prune can remove extraneous packages, but checking the tree and any dry-run support first is better than deleting files as part of a version query.

What does unmet dependency mean? A required dependency is missing or otherwise not satisfied. npm may report other invalid-tree conditions too. Read the specific error and compare the manifest, lockfile, workspace configuration, and installation logs. Preserve the lockfile while diagnosing; deleting it can resolve a different tree and hide the original problem.

How do I learn why a package is installed? npm explain lodash (also available as npm why lodash) shows the dependency relationships that led to its installation.

Can I use this in a script? npm ls --json provides structured output. Account for nonzero exit status when npm reports dependency problems; the output may still contain useful diagnostic data.

References

Get a tailored plan

Contact us

Join the community

Scan with WeChat
to join the community

Try Guance

Start online and pay only for what you use.

Get started

Choose a Guance plan

Code hosting