问题
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