Node JS NPM

npm self update – Selfupdate your global NPM package

nodejs npm
Written by VithalReddy.M.B

npm is the package manager for JavaScript and the world’s largest software registry.Daily thousands of packages downloaded using npm and It would be really good to auto-update npm or self update npm. Isn’t it an awesome hack? Yes, it is.all thanks jviotti. Who developed a node package to auto update npm globally using command line command, we will discuss it later.

 

How to Install selfupdate npm package?

It’s quite easy to install it using npm command and we will -g global command to install it as the global package:

$ npm i selfupdate -g

if you want to use it as a local package and to save in package.json :

npm i selfupdate --save

 

Steps to npm self update a globally installed NPM package:

Step by step procedure to self-update npm on windows, Linux, Ubuntu, mac os.

syntax:

selfupdate.update(packageJSON, callback)
  1. First, you’ll require selfupdate.
    const selfupdate = require('selfupdate');
    
  2. require  package.json file we will require it using require method.
    const pkgJason = require('./package.json');
  3. The callback function will two arguments error and version. Where version is the new version after updating npm.
  4. full code:
    const selfupdate = require('selfupdate');
    const pkgJson = require('./package.json');
     
    selfupdate.update(pkgJson, function(error, version) {
            if(error) throw error;
            console.log('The package was updated to : ' + version + 'version');
    });
  5. If it throws an error, please check your permissions and try again.
  6. To check npm is in the latest version, we will use isUpdated() method on selfupdate. Its callback will also have two arguments error and isUpdated. isUpdated is boolean that will show true or false about the latest version of NPM.
    const selfupdate = require('selfupdate');
    const pkgJson = require('./package.json');
     
    selfupdate.isUpdated(pkgJson , function(error, isUpdated) {
            if(error) throw error;
            console.log('Is the package up to date? :' + isUpdated);
    });

 

READ  How to automatically build the package.json file for NodeJS Projects

That’s it, folks.If you have any queries, please comment below.

 

About the author

VithalReddy.M.B

Vithalreddy.M.B is the Editor of VMRworld.com & stackFame.com with a focus on startups, Startup News, Business, and Technology.He is also an Electrical & Electronics Engineer, Technology Lover, and Entrepreneur by choice.Started His first startup at 21.