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 itselfnpm version <package-name>prints a cryptic errornpm view <package-name> versionprints the package version on the registry (latest available)
What is the correct command to get the installed version of an npm package?
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
- Checking Globally Installed Packages
- Alternative Methods and Options
- Common Issues and Troubleshooting
- Practical Examples
Basic Command for Installed Versions
The primary command to check the installed version of an npm package is:
npm list <package-name>
Or using its alias:
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:
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:
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:
npm list -g <package-name>
npm ls -g <package-name>
For example:
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:
npm pkg get dependencies.<package-name>
For example:
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:
npm ls --json <package-name> | jq '.dependencies."<package-name>".version'
Or simpler:
cat package.json | jq -r '.dependencies."<package-name>"'
Using npm outdated
The npm outdated command shows both installed and latest versions:
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:
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):
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:
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:
- Make sure the package is actually installed in your project
- Check if you’re in the correct directory (run from project root)
- For global packages, ensure they’re installed globally
Long Dependency Trees
The dependency tree can be very long for some projects. To limit output:
npm list <package-name> --depth=1
Practical Examples
Here are some practical examples you can use immediately:
Example 1: Check Express Version
npm list express
# or
npm ls express
Example 2: Check All Top-Level Packages
npm list --depth=0
Example 3: Check React Version Specifically
npm list react --depth=0
Example 4: Check Global Version of TypeScript
npm list -g typescript
Example 5: Get Version as JSON Output
npm list react --json | jq '.dependencies.react.version'
Example 6: Check Multiple Packages at Once
for package in express react lodash; do
npm list $package --depth=0
done
Sources
- npm Documentation - npm list
- npm Documentation - npm pkg get
- npm Documentation - npm ls
- Stack Overflow - How to check installed npm package version
- 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.