NeuroAgent

How to Check Installed npm Package Version

Learn the correct npm commands to find installed package versions. Discover how to use npm list, npm ls, and other methods to check package versions locally and globally. Get practical examples and troubleshooting tips.

Question

How can I find the version of an installed npm package?

I’ve tried several approaches but none seem to work correctly:

  • npm -v <package-name> prints the version of npm itself
  • npm version <package-name> prints a cryptic error
  • npm view <package-name> version prints the package version on the registry (latest available)

What is the correct command to get the installed version of an npm package?

NeuroAgent

The correct command to find the version of an installed npm package is npm list <package-name> or its alias npm ls <package-name>. This command displays the installed version of the package along with its dependency tree. You can also use npm list --depth=0 to show only top-level packages with their versions, or npm list -g <package-name> to check globally installed packages.

Contents

Basic Command for Installed Versions

The primary command to check the installed version of an npm package is:

bash
npm list <package-name>

Or using its alias:

bash
npm ls <package-name>

This command will display:

  • The installed version of the specified package
  • The location where the package is installed
  • The dependency hierarchy (unless you use --depth=0)

For example:

bash
npm list express

Output might look like:

my-project@1.0.0 /path/to/project
└── express@4.18.2

The version number 4.18.2 is what you’re looking for.

To see only the installed version without the dependency tree:

bash
npm list <package-name> --depth=0

This gives you a cleaner output focused just on the installed version information.

Checking Globally Installed Packages

For packages installed globally (using npm install -g <package>), you need to add the -g flag:

bash
npm list -g <package-name>
npm ls -g <package-name>

For example:

bash
npm list -g node

This will show you the globally installed version of Node.js.

Alternative Methods and Options

Several alternative approaches can help you find package versions:

Using npm pkg get

The npm pkg get command can retrieve the version from package.json:

bash
npm pkg get dependencies.<package-name>

For example:

bash
npm pkg get dependencies.express

This would output something like:

"4.18.2"

Checking package.json Directly

You can also check the package.json file directly:

bash
npm ls --json <package-name> | jq '.dependencies."<package-name>".version'

Or simpler:

bash
cat package.json | jq -r '.dependencies."<package-name>"'

Using npm outdated

The npm outdated command shows both installed and latest versions:

bash
npm outdated

This will list packages that have newer versions available, showing your current version versus the latest.

Using npm view with Installed Version

You mentioned that npm view <package-name> version shows the registry version. To get the installed version directly, you can combine commands:

bash
npm list <package-name> --depth=0 --json | jq -r '.dependencies."<package-name>"'

Common Issues and Troubleshooting

Permission Errors

If you encounter permission errors when checking global packages, you might need to use sudo (though this is generally not recommended):

bash
sudo npm list -g <package-name>

Better approach: Configure npm to use a directory where you have write permissions.

Multiple Node Versions

If you have multiple Node versions installed, make sure you’re using the correct npm instance:

bash
which npm
which node

This shows which npm and Node.js executables are being used.

Package Not Found Errors

If you get “package not found” errors:

  1. Make sure the package is actually installed in your project
  2. Check if you’re in the correct directory (run from project root)
  3. For global packages, ensure they’re installed globally

Long Dependency Trees

The dependency tree can be very long for some projects. To limit output:

bash
npm list <package-name> --depth=1

Practical Examples

Here are some practical examples you can use immediately:

Example 1: Check Express Version

bash
npm list express
# or
npm ls express

Example 2: Check All Top-Level Packages

bash
npm list --depth=0

Example 3: Check React Version Specifically

bash
npm list react --depth=0

Example 4: Check Global Version of TypeScript

bash
npm list -g typescript

Example 5: Get Version as JSON Output

bash
npm list react --json | jq '.dependencies.react.version'

Example 6: Check Multiple Packages at Once

bash
for package in express react lodash; do
  npm list $package --depth=0
done

Sources

  1. npm Documentation - npm list
  2. npm Documentation - npm pkg get
  3. npm Documentation - npm ls
  4. Stack Overflow - How to check installed npm package version
  5. GitHub npm - List command implementation

Conclusion

The most reliable way to check the installed version of an npm package is using npm list <package-name> or npm ls <package-name>. Remember to use the --depth=0 flag for cleaner output and -g flag for globally installed packages. If you need programmatic access, consider using npm pkg get or JSON output with --json. For complex projects with many dependencies, the dependency tree visualization can help understand version conflicts and upgrade paths. Always run these commands from your project root directory to ensure you’re checking the correct package versions for your specific project context.