Angelos Chalaris
Angelos Chalaris

Reputation: 6707

Get NPM version tag using client-side javascript

I was wondering if there is any way to get the latest version tag of a NPM package, using client-side JavaScript. I looked around for APIs and the like but I couldn't find anything that isn't deprecated.

However, I am pretty certain that it's possible to do, as shields.io does exactly that for multiple packages.

What I want to do is get the latest version tag as-is (e.g. v1.2.5) in either string or JSON format.

Upvotes: 1

Views: 122

Answers (1)

Jamie Dixon
Jamie Dixon

Reputation: 54021

You can call into the npm registry using your AJAX method of choice.

The URL for a given package is the base registry URL http://registry.npmjs.org with the package as the path i.e. http://registry.npmjs.org/styled-components.

If you check out that second URL you'll see a JSON document containing all the information you want for that package including the latest version.

example code:

const res = await fetch('http://registry.npmjs.org/styled-components');
const json = await res.json();
console.log(json['dist-tags'].latest) // latest distributed version

Upvotes: 3

Related Questions