Table of Contents
Checking the version of an installed npm package is a common task in Node.js development. You may need to verify the version of a package to ensure compatibility with your code or to determine if an update is available. In this answer, we will explore two methods to check the version of an installed npm package.
Method 1: Using the "npm list" command
One way to check the version of an installed npm package is by using the "npm list" command. This command displays a tree-like structure of all installed packages and their dependencies. To check the version of a specific package, you can use the "--depth=0" flag to limit the output to only the top-level packages.
Here is the command to check the version of an installed npm package using the "npm list" command:
npm list <package-name> --depth=0
Replace <package-name>
with the name of the package you want to check. For example, to check the version of the "express" package, you would run:
npm list express --depth=0
This command will display the version of the "express" package installed in your project.
Related Article: How To Update Node.Js
Method 2: Using the "npm show" command
Another method to check the version of an installed npm package is by using the "npm show" command. This command provides detailed information about a package, including its version, description, and other metadata. By specifying only the package name, you can retrieve the version information.
Here is the command to check the version of an installed npm package using the "npm show" command:
npm show <package-name> version
Replace <package-name>
with the name of the package you want to check. For example, to check the version of the "lodash" package, you would run:
npm show lodash version
This command will display the version of the "lodash" package installed in your project.
Best Practices
Related Article: How To Check Node.Js Version On Command Line
When checking the version of an installed npm package, it is recommended to follow these best practices:
1. Specify package versions in your project's "package.json" file to ensure consistent and reproducible builds. This allows you to easily track and update dependencies.
2. Regularly check for updates of your project's dependencies by running the "npm outdated" command. This command shows a list of outdated packages and their current and latest versions.
3. Use version ranges in your "package.json" file to allow for flexibility in updating packages. For example, specifying "^1.0.0" allows for any minor or patch version update, while restricting major version updates.
4. Consider using a package manager like "npx" to run scripts without having to install packages globally. This can help avoid version conflicts and ensure consistent package versions across different projects.
5. Take advantage of the "npm audit" command to identify and fix security vulnerabilities in your project's dependencies. This command analyzes your project's dependencies and provides recommendations for updating vulnerable packages.