How to update npm version
How to update npm version
NPM (Node Package Manager) is the default package manager for Node.js and is written entirely in JavaScript. It manages all the packages and modules for Node.js and consists of command-line client npm. It gets installed into the system with the installation of Node.js. The required packages and modules in the Node project are installed using NPM.
The update of the NPM means the update node package manager to the latest version. The update of NPM updates the Node.js and modules to the latest version.
Method 1: Using npm update command to update the node package manager.
Method 2: Using npm@latest command to update the node package manager.
Method 3: Using PPA repository (only for Linux).
Method 4: Using cache cleaning & stable installing (only for Linux).
Method 5: Using npm@next to update the node package manager.
How to update globally installed npm packages
How do I update these outdated packages in npm?
3 Answers 3
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
If you want to update all global packages
If you want to update specific global package
To automatically update all global packages to the ‘Latest’ version:
That will update all global packages to the ‘Latest’ version. More information is available about npm-check, including the ability to perform an interactive update, exclude packages, etc.
Lastly, if you want to update (install) a package to a version other than ‘Latest’ or ‘Wanted’:
To add to Mohit’s answer, if you’re using NPM 2.6 or less, there are a couple scripts that are handy in handling the update in that scenario: https://gist.github.com/othiym23/4ac31155da23962afd0e.
You’ll need to create the two files described, and run them from the command prompt. This will update all the packages. For a selective update, scroll down the page at the above link to Dylang’s comment from October 20, 2014. The comment below from Nov 6, 2014 will hook you up with scripts for a Windows environment.
Looking at long term maintenance, your best solution might be to update NPM first by running:
npm check and update package if needed
We need to integrate Karma test runner into TeamCity and for that I’d like to give sys-engineers small script (powershell or whatever) that would:
pick up desired version number from some config file (I guess I can put it as a comment right in the karma.conf.js )
check if the defined version of karma runner installed in npm’s global repo
if it’s not, or the installed version is older than desired: pick up and install right version
I don’t want to always check and install the latest available version, because other config values may become incompatible
17 Answers 17
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
To check if any module in a project is ‘old’:
‘outdated’ will check every module defined in package.json and see if there is a newer version in the NPM registry.
For example, say xml2js 0.2.6 (located in node_modules in the current project) is outdated because a newer version exists (0.2.7). You would see:
To update all dependencies, if you are confident this is desirable:
Or, to update a single dependency such as xml2js :
npm outdated will identify packages that should be updated, and npm update
can be used to update each package. But prior to npm@5.0.0, npm update
will not update the versions in your package.json which is an issue.
The best workflow is to:
Check out npm-check-updates to help with this workflow.
There is also a «fresh» module called npm-check :
Check for outdated, incorrect, and unused dependencies.
That is all. All of the package versions in package.json will be the latest major versions.
What is happening here?
Installing a package that checks updates for you.
Install all of the new versions of the packages.
To update a single local package:
First find out your outdated packages:
Then update the package or packages that you want manually as:
This way it is not necessary to update your local package.json file.
Note that this will update your package to the latest version.
If you write some version in your package.json file and do:
npm update package_name
In this case you will get just the next stable version (wanted) regarding the version that you wrote in your package.json file.
And with npm list (package_name) you can find out the current version of your local packages.
You can try either of these options:
Check outdated packages
Check and pick packages to update
No additional packages, to just check outdated and update those which are, this command will do:
NPM commands to update or fix vulnerabilities in some dependency manifest files
Use below command to check outdated or vulnerabilities in your node modules.
If any vulnerabilities found, use below command to fix all issues.
If it doesn’t work for you then try
If you don’t want to use force audit fix then you can manually fix your dependencies versions by changing them in package-lock.json and package.json file. Then run
npm update && npm upgrade
When installing npm packages (both globally or locally) you can define a specific version by using the @version syntax to define a version to be installed.
npm-upgrade
Interactive CLI utility to easily update outdated NPM dependencies with changelogs inspection support.
What is this for?
If you are tired of manually upgrading package.json every time your package dependencies are getting out of date then this utility is for you.
Take a look at this demo:
Installation
First, install Node.js (at least v10.19 ).
Then install this utility as global npm-module:
Usage
check command
If you want to check only some deps, you can use filter argument:
If you want to check only a group of deps use these options:
Ignoring module
You can handle these situations by ignoring such modules. You can do it in two ways: choosing Ignore during update process or using npm ignore add command.
You will asked two questions. First is a version range to ignore. It should be a valid semver version. Here are a few examples:
And after that npm-upgrade will ask about the ignore reason. The answer is optional but is strongly recommended because it will help to explain your motivation to your СЃolleagues and to yourself after a few months.
ignore command
It will help you manage ignored modules. See Ignoring module section for more details. It has the following subcommands:
changelog command
Will try to find changelog url for provided module and open it in default browser.
Troubleshooting
Wrong changelog shown for or not shown at all!
Yes, It can happen sometimes. This is because there is no standardized way to specify changelog location for the module, so it tries to guess it, using these rules one by one:
How to Update NPM Packages
Node Package Manager, or npm, gets installed along with Node.js and you can use it to import software packages built on top of Node.js.
Because the language is already very popular and easy to use. There are now a myriad of applications built on top of the Node.js to the point that it almost has its own ecosystem of applications and frameworks to build applications each bundled as its own package. The problem with that arises when developers start to improve upon their packages and releases new version.
Like most package managers, npm installs the most recent (stable) release of a package. So if you install a particular version of Express for your web application and a few years later after a new version of Express, chances are somethings in your app might break in a direct or indirect way.
To circumvent this issue, it is handy to use npm in a way that makes use of version numbers to keep track of packages.
Installing Node.js and npm
If you don’t already have Node.js installed in your system it is a good idea to start with the official LTS release. The official repository of Ubuntu 16.04 is a bit behind from the latest stable release and we would thus add the Node.js official repo as a PPA.
The above command fetches a shell script from deb.nodesource.com and runs it via bash. The script automatically adds the public keys of the repo and updates the source lists for apt package manager.
After this installing Node.js and npm is as easy as running:
Now before we get started with installing and updating various packages, let’s make sure that the version of npm and Node.js is what we desire them to be.
The version of npm is not the latest stable release (although we made sure that Node.js is the LTS version)
To update npm itself you can run the command:
If you would like to go back you can enter the previous version number in a similar fashion. For example:
Installing and updating local packages
npm offers 2 different methods for installing packages. First is locally to be used in, say, another software that you are trying to build and the second method for installing it is to do it across the system for all users. This is great if you are trying to install a system utility, say with a CLI interface, to be used as a basic command or a standalone software.
The local packages are directory specific. For example, if you are building a Node.js app first make project folder or directory:
Now, from inside the directory you can run an npm init command and enter appropriate values for the name, description, git repository and other relevant fields which are the metadata of the package.
Now if you install a package, such as lodash by running the following command in the directory:
npm automatically gets the latest stable release of the said package and installs it for you.
If you want to check the version number then, enter the command:
If a new version of lodash comes along and you wish to upgrade then, in the same directory, run:
Now you can see that the version number is greater than what it was before.
If something is wrong with the new version and you are facing issues, then you can always go back to previous state by simply running.
Of course, that would require keeping track of the previous version number. Performing a git commit before any of the npm update actions is advisable for this reason.
Installing and updating global packages
Installing packages globally is actually much simpler since these are usually standalone utility. A great example of such a package is gtop which is similar to the top utility in Linux. It shows all the resource utilization in a clean and elegant way.
To install a package globally, either be the root user or prefix your command with sudo if you are a user with root previleges.
Now, like any command utility, you can call upon gtop by running:
If you want to update the package,then simply run:
And you are done!
Conclusion
So that’s basic package management if you want to use npm packages for your new software project or as a standalone system utility. If you wish to explore npm packages you can always do so by going to their official website.
About the author
Ranvir Singh
I am a tech and science writer with quite a diverse range of interests. A strong believer of the Unix philosophy. Few of the things I am passionate about include system administration, computer hardware and physics.