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.
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
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);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With