节点:以编程方式检查包的最新版本 [英] Node: check latest version of package programmatically

查看:48
本文介绍了节点:以编程方式检查包的最新版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的节点包(在 npm 上发布)在新版本可用时提醒用户.如何以编程方式检查已发布包的最新版本并将其与当前版本进行比较?

I'd like my node package (published on npm) to alert the user when a new version is available. How can i check programmatically for the latest version of a published package and compare it to the current one?

谢谢

推荐答案

你可以结合 npmview(用于获取远程版本)和 semver(用于比较版本)包来做到这一点:

You can combine the npmview (for getting remote version) and semver (for comparing versions) packages to do this:

const npmview = require('npmview');
const semver  = require('semver');

// get local package name and version from package.json (or wherever)
const pkgName    = require('./package.json').name;
const pkgVersion = require('./package.json').version;

// get latest version on npm
npmview(pkgName, function(err, version, moduleInfo) {
  // compare to local version
  if(semver.gt(version, pkgVersion)) {
    // remote version on npm is newer than current version
  }
});

这篇关于节点:以编程方式检查包的最新版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆