Terminus
Check Npm Package Version

Check Npm Package Version

The short answer

To check the currently installed version (i.e. the “node modules version”) of a specific package in your project, you can use the [.inline-code]npm ls[.inline-code] command (alias for [.inline-code]npm list[.inline-code]) as follows:

 $ npm ls <package-name>

For example, if running [.inline-code]npm ls msw[.inline-code] outputs [.inline-code]msw@0.42.1[.inline-code], it means that the version [.inline-code]0.42.1[.inline-code] of the [.inline-code]msw[.inline-code] package has been installed within the [.inline-code]node_modules[.inline-code] directory of your project.

 $ npm ls msw
testing-react-applications-workshop@1.0.0 /epic-react/testing-react-apps
└── msw@0.42.1

You can get the list of available [.inline-code]npm[.inline-code] versions on the official npmjs website.

[#check-if-package-is-installed][.inline-code]npm[.inline-code] check if the package is installed at all [#check-if-package-is-installed]

On the other hand, if running [.inline-code]npm ls testcafe[.inline-code] outputs [.inline-code](empty)[.inline-code], it means that this package has not been installed within your project.

 $ npm ls testcafe
testing-react-applications-workshop@1.0.0 /epic-react/testing-react-apps
└── (empty)

We have another post on npm list installed packages which explores how to view all of the installed packages both locally and globally. 

[#easily-recall-with-ai]Easily retrieve this command using Warp’s AI Command Search[#easily-recall-with-ai]

If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Search feature:

This feature is a game-changer and it allows developers to maintain flow and easily look up commands without leaving their terminal.

Typing [.inline-code]#[.inline-code] and entering [.inline-code]check package version[.inline-code] in the AI Command Search will prompt a [.inline-code]npm list[.inline-code] command that can then quickly be inserted into your shell by doing [.inline-code]CMD+ENTER[.inline-code].

[#json-vs-node]The difference between the [.inline-code]package.json[.inline-code] file and the [.inline-code]node_modules[.inline-code] folder[#json-vs-node]

Many developers tend to rely on the [.inline-code]package.json[.inline-code] file to determine the version of an  installed package. We would recommend using the commands listed above instead. The [.inline-code]package.json[.inline-code] file is used to map the package to a version range and note the project’s requirements. The [.inline-code]node_modules[.inline-code] folder contains the installed version of the package and represents the exact version of the code that has been downloaded.

 // package.json file
{
  "name": "project-name",
  "version": "0.0.",
  "dependencies": {
    ….
    "lodash": "^4.17.21"
  }
}

In the above example, the [.inline-code]lodash[.inline-code] version contains a caret ([.inline-code]^[.inline-code]) operator. This indicates that the project is compatible with any [.inline-code]lodash[.inline-code] version greater than [.inline-code]4.17.21[.inline-code] but less than [.inline-code]5.0.0[.inline-code]. Thus, the version installed within the [.inline-code]node_modules[.inline-code] directory can be [.inline-code]4.17.25[.inline-code] and it'll still fall within the [.inline-code]package.json[.inline-code] specifications.

 $ which npm

[#what-is-npm-view]View package information[#what-is-npm-view]

To view information about a package available on the npm registry, you can use the following command:

 $ npm view <package-name>

Which will, by default, output information about its latest version.

To view information about a package at a specific version, you can append the version number to the package name as follows:

 npm view react@17.0.2

[#list-package-versions]Available versions of a package[#list-package-versions]

In order to view all of the available versions of a specific package, you can append the [.inline-code]versions[.inline-code] argument to the [.inline-code]npm view[.inline-code] command as follows:

 $ npm view <package-name> versions

[#what-is-npm-check]Using npm-check [#what-is-npm-check]

If your project contains many dependencies and you prioritize actively maintaining them, you might want to consider using the [.inline-code]npm-check[.inline-code] package to manage them.

The [.inline-code]npm-check[.inline-code] library provides a user-friendly command line interface to check for incorrect dependencies, remove unused packages, update modules, and view package details. It also has an interactive interface which prevents user errors like typos.

However, if your project is relatively small, then the npm CLI commands should be more than enough for your use case. For example, if you wanted to update your packages, you can leverage the existing [.inline-code]npm update[.inline-code] command. And as mentioned above, the [.inline-code]npm view[.inline-code] command can give you more details about a package.