Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NPM: how to get current latest version of package from code

Tags:

npm

I'm developing a npm package (a CLI command) and I want to to display something to the console to indicate that they're not using the latest version. How do I get the version number of a package on npm from code (JS)? I'd rather not parse html the standard page, so I was wondering if there's an API.

like image 872
Bronco Oostermeyer Avatar asked Oct 21 '25 05:10

Bronco Oostermeyer


2 Answers

You can use npm registry API with a GET on /{package} to query for package information, including the latest version published. Once you have that, just check against the local version.

Details here: https://github.com/npm/registry/blob/master/docs/REGISTRY-API.md#getpackage

like image 169
Victor Nascimento Avatar answered Oct 24 '25 11:10

Victor Nascimento


I am so late to answer this question but I found a way to get the package version using UNPKG.

Just change the name of the package to the package you want to get the version using the package.json file then use fetch or axios to get the data and show it on your website.

I am using Reactjs in this example.

axios
  .get("https://unpkg.com/kvass/package.json")
  .then((response) => {
    setVersion(response.data.version);
  });
like image 26
Aiman Almureish Avatar answered Oct 24 '25 10:10

Aiman Almureish