How to Check the Version of an Installed npm Package

Avatar

By squashlabs, Last Updated: Oct. 1, 2023

How to Check the Version of an Installed npm Package

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.

You May Also Like

Building a Language Learning Game with GraphQL & Nodejs

This tutorial teaches readers how to create a language learning game using GraphQL, Node.js, React.js, MongoDB, and Docker. The article covers topics… read more

Integrating HTMX with Javascript Frameworks

Integrating HTMX with JavaScript frameworks is a valuable skill for frontend developers. This article provides best practices for using HTMX with pop… read more

How to Implement Sleep in Node.js

Node.js is a powerful runtime environment for JavaScript that allows developers to build scalable and high-performance applications. One common requi… read more

How to Run 100 Queries Simultaneously in Nodejs & PostgreSQL

Learn how to execute 100 queries simultaneously in Node.js with PostgreSQL. Understand how PostgreSQL handles executing multiple queries at once and … read more

How to Fix “nvm command not found” with Node Version Manager

A guide on fixing the "nvm command not found" issue while installing Node Version Manager. Learn how to update your shell configuration, reinstall No… read more

How To Downgrade To A Previous Node Version In Nodejs

Guide on downgrading to an earlier Node version in Nodejs. Learn how to use Node Version Manager (NVM) and manually install older versions. Additiona… read more

How to Resolve the Npm Warnings with Config Global & Local

Node.js developers often encounter warnings related to the deprecated npm config global "--global" and "--local" flags. These warnings can be confusi… read more

Big Data Processing with Node.js and Apache Kafka

Handling large datasets and processing real-time data are critical challenges in today's data-driven world. In this article, we delve into the power … read more

How to Fix “Npm Err! Code Elifecycle” in Node.js

A quick guide to fix the "Npm Err! Code Elifecycle" error in Node.js applications. Learn how to resolve the issue by checking your package.json file,… read more

Advanced DB Queries with Nodejs, Sequelize & Knex.js

Learn how to set up advanced database queries and optimize indexing in MySQL, PostgreSQL, MongoDB, and Elasticsearch using JavaScript and Node.js. Di… read more