Node: check latest version of package programmatically

血红的双手。 提交于 2021-01-27 05:30:14

问题


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?

Thanks


回答1:


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
  }
});


来源:https://stackoverflow.com/questions/44699595/node-check-latest-version-of-package-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!