简体   繁体   English

使用客户端javascript获取NPM版本标记

[英]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. 我想知道是否有任何方法可以使用客户端JavaScript获取NPM包的最新版本标签。 I looked around for APIs and the like but I couldn't find anything that isn't deprecated. 我四处寻找API等,但我找不到任何不被弃用的东西。

However, I am pretty certain that it's possible to do, as shields.io does exactly that for multiple packages. 但是,我很确定它可以做到,因为shields.io完全适用于多个包。

What I want to do is get the latest version tag as-is (eg v1.2.5 ) in either string or JSON format. 我想要做的是以字符串或JSON格式获取最新版本标签(例如v1.2.5 )。

You can call into the npm registry using your AJAX method of choice. 您可以使用您选择的AJAX方法调用npm注册表。

The URL for a given package is the base registry URL http://registry.npmjs.org with the package as the path ie http://registry.npmjs.org/styled-components . 给定包的URL是基本注册表URL http://registry.npmjs.org ,包为路径,即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. 如果您查看第二个URL,您将看到一个JSON文档,其中包含您对该软件包所需的所有信息,包括最新版本。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM