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.
Article Contents
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)
- First, you’ll require selfupdate.
const selfupdate = require('selfupdate');
- require package.json file we will require it using require method.
const pkgJason = require('./package.json');
- The callback function will two arguments error and version. Where version is the new version after updating npm.
- 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'); });
- If it throws an error, please check your permissions and try again.
- 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); });
That’s it, folks.If you have any queries, please comment below.