如何以编程方式检查已安装的 Node.js 版本的发布状态?
我想在安装脚本中包含一个检查,以验证安装的 Node.js 版本是否为 LTS。像这样的东西将是理想的
$ node -vvv
v16.3.0
Release v16
Status Active LTS
Codename Gallium
Initial Release 2021-04-20
Active LTS Start 2021-10-26
Maintenance LTS Start 2022-10-18
End-Of-Life 2024-04-30
基本上某种方式来获取 https://nodejs.org/ 上可用的信息en/about/releases/ 但在脚本中。如果有某个 HTTP 端点以 JSON 形式提供这些内容,那也可以。
我查看了 node
、npm
、nvm
和 yarn
CLI 工具中的可用选项,但没有一个他们似乎可以做到这一点。
I want to include a check in a setup script that validates the installed node.js version is LTS. Something like this would be ideal
$ node -vvv
v16.3.0
Release v16
Status Active LTS
Codename Gallium
Initial Release 2021-04-20
Active LTS Start 2021-10-26
Maintenance LTS Start 2022-10-18
End-Of-Life 2024-04-30
Basically some way to get the information available on https://nodejs.org/en/about/releases/ but in a script. If there's some HTTP endpoint that provides this stuff in JSON that would work too.
I've looked through available options in the node
, npm
, nvm
, and yarn
CLI tools but none of them seem like they can do this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有关 NodeJS 版本的 JSON 信息来源之一是此端点:
https://nodejs.org/download/release /index.json
在那里,您将获得一个对象数组,每个对象如下所示:
如果您通过
lts
属性(除false
) 然后按版本排序,您将找到最新的 LTS 发行版本,并且您可以将其与本地安装的进行比较。下面是一段 Nodejs 代码,它获取 JSON,过滤掉不是
lts
的项目,然后按版本排序。结果数组的顶部将是最新的 LTS 发行版本号。然后,您可以通过编程方式将其与本地安装的内容进行比较。您可以仅使用此脚本中的process.version
,也可以运行一个从node -v
捕获输出的 child_process。当我此时在我的系统上运行它时,我得到以下输出:
One source of JSON info about nodejs releases is this endpoint:
https://nodejs.org/download/release/index.json
There, you get an array of objects that each look like this:
If you filter that array by the
lts
property (any value other thanfalse
) and then sort by version, you will find the latest LTS release version and you can compare that to what is installed locally.Here's a piece of nodejs code that gets that JSON, filters out items that aren't
lts
, then sorts by version. The top of the resulting array will be the latest LTS release version number. You could then programmatically compare that to what is installed locally. You could either just useprocess.version
from within this script or you could run a child_process that captures the output fromnode -v
.When I run this on my system at this moment, I get this output: