Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node: check latest version of package programmatically

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

like image 369
pistacchio Avatar asked Jun 22 '17 12:06

pistacchio


People also ask

How do I check node package version?

To discover npm version checks are currently installed in your project, run npm list. All the npm modules available are: the latest version of [email protected]. This [email protected].

How do I check if I have the latest version of node?

To see if Node is installed, open the Windows Command Prompt, Powershell or a similar command line tool, and type node -v . This should print the version number so you'll see something like this v0. 10.35 .


1 Answers

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
  }
});
like image 106
Frxstrem Avatar answered Oct 04 '22 05:10

Frxstrem