2171

I just installed Node.js and npm. I installed npm for access to additional Node.js modules. After I installed Node.js and npm, I noticed that neither were the latest versions available.

So my questions are:

  • How do I upgrade Node.js, npm and my Node.js modules to their latest versions?
  • Do I need to uninstall Node.js and npm, and reinstall the latest versions?
11

67 Answers 67

2162

How to update npm

Use the following command:

npm update -g npm

See the documentation for the update command:

npm update [-g] [<pkg>...]

This command will update all the packages listed to the latest version (specified by the tag config), respecting semver.

Additionally, see the documentation on Node.js and npm installation and Upgrading npm.

The following original answer is from the old FAQ that no longer exists, but it should work for Linux and Mac:

How do I update npm?

npm install -g npm

Please note that this command will remove your current version of npm. Make sure to use sudo npm install -g npm if on a Mac.

You can also update all outdated local packages by doing npm update without any arguments, or global packages by doing npm update -g.

Occasionally, the version of npm will progress such that the current version cannot be properly installed with the version that you have installed already. (Consider, if there is ever a bug in the update command.) In those cases, you can do this:

curl https://www.npmjs.com/install.sh | sh

How to update Node.js

To update Node.js itself, I recommend you use nvm (Node Version Manager). Here is the quote from the official nmp documentation:

We strongly recommend using a Node version manager like nvm to install Node.js and npm. We do not recommend using a Node installer, since the Node installation process installs npm in a directory with local permissions and can cause permissions errors when you run npm packages globally.

Here is how you can do it using nvm tool:

  • To install the latest release of Node, do this:

    nvm install node
    

    (NOTE: here node is an alias for the latest version)

  • To install a specific version of node:

    nvm install 22.2.0
    

More details can be found on the official nvm GitHub page in the "Usage" section.

26
  • 174
    npm update npm -g didn't work for me on windows - it completed without output but npm remained the same version (1.3.11 when the most recent version is 1.3.14)
    – B T
    Commented Nov 16, 2013 at 20:24
  • 14
    also have curl follow redirects with -L curl -L https://npmjs.org/install.sh | sudo sh
    – AndyL
    Commented Apr 15, 2014 at 20:03
  • 45
    I did it like you said npm update npm -g and now I don't have npm as a command.
    – alexserver
    Commented Jun 23, 2014 at 17:23
  • 32
    Using npm to install a new version of itself. I smell things blowing up. Commented Jan 26, 2016 at 4:53
  • 26
    DON'T use sudo npm install -g npm! This will uninstall it!
    – chris123
    Commented Oct 16, 2016 at 17:47
529

I found this really neat way of updating Node.js on David Walsh's blog. You can do it by installing n:

sudo npm cache clean -f
sudo npm install -g n
sudo n stable

It will install the current stable version of Node.js.


UPDATE

But please don't use n anymore. I recommend using nvm. You can simply install stable by following the commands below:

nvm ls-remote
nvm install <version>
nvm use <version>
16
  • 11
    Refuses to run for windows x64 (but does with win32 apparently). That's not in keeping with the Node.js project.
    – rainabba
    Commented Jan 29, 2015 at 0:10
  • 10
    Not supported for Windows. I realize it would be easier to just use Linux, but at work, I am not free to choose my OS. As @rainabba says, the spirit of the Node.js project is to be cross-platform, and this isn't.
    – Kevin Dice
    Commented Nov 5, 2015 at 19:04
  • 2
    In Windows, you can simply download the newest version of node and install it. It'll upgrade both node and npm. Commented Dec 17, 2015 at 22:13
  • 26
    Why did you said 'Please don't use n anymore' ? Any particular reason ?
    – Qiulang
    Commented Jan 16, 2019 at 4:59
  • 7
    I do not know why David Walsh said n was unstable at the time, but that blog was from six years ago and n is still being maintained. Repeating other commenters, n does not run on Windows. And neither does nvm. Commented Feb 16, 2019 at 22:57
436

Updating npm is easy:

npm install npm@latest -g
8
  • 10
    Using windows 10 and node v5.7.0 the command npm install npm@latest -g failed to upgrade from npm 2.7.4 to npm 3.9.3.
    – surfmuggle
    Commented May 31, 2016 at 19:00
  • 4
    Thanks this worked for me, its pretty simple. But i had to sudo npm install npm@latest -g thats all. Upvoted Commented Aug 9, 2016 at 7:21
  • 24
    This is also the official answer: docs.npmjs.com/getting-started/installing-node Commented Mar 20, 2017 at 23:16
  • 1
    Quit the terminal where you run the npm install -g npm and retry Commented Apr 3, 2021 at 9:31
  • 1
    As in @surfmuggle's case, the command apparently failed silently to update to the next major version (v9.8.1 to the current v10.6.0). However, starting a new terminal did show the correct version. Commented Apr 29 at 23:53
189

I understand this question is for a Linux machine, but just in case anybody is looking for a Windows solution, just go to the Node.js site, click the download button on the homepage and execute the installer program.

Thankfully, it took care of everything, and with a few clicks of the Next button, I got the latest 0.8.15 Node.js version running on my Windows 7 machine.

6
  • 2
    yeah. this worked for me. Im on windows. the n method didnt work. wish there was a better way of doing it.
    – Alexis
    Commented Jun 19, 2013 at 21:19
  • 2
    This is the best and easiest way to upgrade node on windows. worked for me just fine. Thanks anmol
    – peter
    Commented Oct 31, 2013 at 5:01
  • 7
    if you're updating from npm v1.x to 2.x on Windows, you may have delete ‘npm’, ‘npm.cmd’ files from “C:\Program Files\nodejs” after installing or udpating npm. See: escapologist.wordpress.com/2014/09/29/…
    – Tom Wayson
    Commented Feb 5, 2015 at 15:08
  • Don't forget to reboot afterwards! Commented Oct 19, 2015 at 8:41
  • This works with Windows 10, and both Node and npm update. Very slick.
    – Mike_Laird
    Commented Apr 28, 2016 at 18:13
179

As you may already know, npm is currently bundled with Node.js. It means that if you have installed Node.js, you've already installed npm as well.

Also, pay attention to the Node.js and npm release versions table that shows us approximate versions compatibility. Sometimes, versions discrepancy may cause incompatibility errors.

So, if you're a developer, it's kind of "best practice" to manage your development environment using one of the Node.js version managers.

Here is a list and usage notes of some of the most popular:

Homebrew (macOS)

If you're on macOS, you can use Homebrew.

Actually, it's not just a Node.js version manager.

To install Homebrew to your Mac:

ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"

To install Node.js and npm using Homebrew, run:

brew install node

Later, you will be able to update them using:

brew update && brew upgrade node

Also, you can switch between Node.js versions as well:

brew switch node 0.10.26

npm will be upgraded/downgraded automatically.

n (macOS, Linux)

n is most likely to rvm (Ruby Version Manager), and is used to manage Node.js and npm versions simultaneously. It is written on pure Linux shell, and available as an npm module. So, if you already have any Node.js version installed, you can install/update the n package through npm:

npm install -g n

Downloading, installing and switching to Node.js and npm versions is as easy as:

n 0.10.26
n 0.8.17
n 0.9.6

To download, install, and switch to the latest official release, use:

n latest

To download, install, and switch to the latest stable official release, use:

n stable

To switch to the previously active version (aka $ cd -), use:

n prev

If you want to see the list of installed Node.js versions, just run n from your command line. The output will be something like the following:

n

Output:

  0.10.26
• 0.8.17
  0.9.6

Where the dot (•) means that it's a currently active version. To select another Node.js version from the list, use Up/Down arrow keys and activate using the Enter key.

To list the versions available to install:

n lsr

nvm (macOS, Linux)

nvm is also like rvm, even the command names and usage are very similar.

To install nvm, you can use the installation script (requires Git) using cURL:

curl https://raw.github.com/creationix/nvm/master/install.sh | sh

or Wget:

wget -qO- https://raw.github.com/creationix/nvm/master/install.sh | sh

To download and install a specific Node.js and npm version, use:

nvm install 0.10

Then, you can switch to the installed version, using:

nvm use 0.10

Also, you can create the .nvmrc file containing the version number, then switch to the specified version using the following command:

nvm use

To see the list of installed Node.js versions, use:

nvm ls

To list the versions available to install:

nvm ls-remote

nvm-windows (Windows)

nvm-windows is a Node.js version management utility for Windows, ironically written in Go.

It is not the same thing as nvm. However, the usage as a Node.js version manager is very similar.

To install nvm-windows, it is required to uninstall any existing versions of Node.js and npm beforehand. Then, download and run the latest installer from releases.

To upgrade nvm-windows, run the new installer. It will safely overwrite the files it needs to update without touching your Node.js installations.

nvm-windows runs in an Admin shell. You'll need to start PowerShell or Command Prompt as Administrator to use nvm-windows.

Before using, you may also need to enable nvm-windows with the following command:

nvm on

To download and install a specific Node.js and npm version, use:

nvm install 0.12

Then, you can switch to the installed version, using:

nvm use 0.12

If you want to see the list of installed Node.js versions, use:

nvm list

To list the versions available to install:

nvm list available
4
  • 1
    I like this answer. +1 ! But it would have been even more useful to accompany it with some sort of explanation about the different package managers. Ie; why I should use one over the other. I would really help to give context..
    – 7wp
    Commented Oct 19, 2015 at 5:01
  • 1
    with command "brew update && brew upgrade node" , npm doesn't get updated to latest version as mentioned "...NPM will be updated as well.."
    – vikramvi
    Commented Feb 24, 2017 at 10:36
  • 1
    I generally like homebrew but the fact it requires an update before I can upgrade npm on it is painfully slow... Commented Dec 26, 2018 at 18:43
  • can you please explain "npm will be upgraded/downgraded automatically." which command does this ?
    – vikramvi
    Commented Aug 17, 2020 at 5:52
131

First check your NPM version

npm -v

1) Update NPM to current version:

View curent NPM version:

npm view npm version

Update npm to current version:

npm i -g npm


2) List all available NPM versions and make a custom install/update/roll-back

View all versions including "alpha", "beta" and "rc" (release candidate)

npm view npm versions --json

Reinstall NPM to a specific version chosen from the versions list - for example to 5.0.3

npm i -g [email protected]
  • Installing one version will automatically remove the one currently installed.

  • For Linux and iOS prepend commands with sudo

2
  • 3
    I ran into bundle of errors including Error: EACCES: permission denied, unlink running above command with sudo worked
    – Imran Ali
    Commented Oct 23, 2017 at 12:17
  • 1
    @DevWL, I'm trying your solution now. When I ran the npm view npm versions --json command, I got a TON of listings of versions beginning at "1.4.26" - "7.0.0-beta.12" so I'm running npm i -g [email protected] -- it appears to be taking a while (currently fetch -> lock is running but looks hung) fingers-crossed
    – Chris22
    Commented Sep 27, 2020 at 12:44
67

Upgrading for Windows Users

Windows users should read Troubleshooting > Upgrading on Windows in the npm wiki.

Upgrading on windows 10 using PowerShell (3rd party edit)

The link above Troubleshooting#upgrading-on-windows points to a github page npm-windows-upgrade the lines below are quotes from the readme. I successfully upgraded from npm 2.7.4 to npm 3.9.3 using node v5.7.0 and powershell (presumably powershell version 5.0.10586.122)

First, ensure that you can execute scripts on your system by running the following command from an elevated PowerShell. To run PowerShell as Administrator, click Start, search for PowerShell, right-click PowerShell and select Run as Administrator.

Set-ExecutionPolicy Unrestricted -Scope CurrentUser -Force    

Then, to install and use this upgrader tool, run (also from an elevated PowerShell or cmd.exe):

npm install --global --production npm-windows-upgrade
npm-windows-upgrade
2
  • or just run: npm install -g npm Commented Mar 22, 2016 at 9:38
  • 1
    No Gal, you are confusing the issue, running npm install -g npm does not work on Windows, have you read the issues Windows users have had (listed above ?). Use the Powershell method as shown in Surf's answer. Thanks smurf , finally managed to upgrade yeeehaaaa!
    – Davet
    Commented Aug 3, 2016 at 9:02
66

First update npm,

npm install -g npm@next

Then update node to the next version,

npm install -g node@next or npm install -g n@next or, to the latest,

npm install -g node@latest or npm install -g node

check after version installation,

node --versionor node -v

0
59

SIMPLY USE THIS

npm i -g npm

This is what i get promped on my console from npm when new update/bug-fix are released:

enter image description here

1
  • 1
    after I ran this I had to exit the bash terminal and restart to see the changes. Because the path to npm changed during the update process and for some reason it kept executing the old version which showed the older version of course when you executed npm -v Commented Jul 9, 2018 at 0:22
33
  • To update Node.js, use nvm (or nvmw for Windows).

  • To update npm, the npm update npm -g command didn't work for me (on Windows). What did work was reinstalling npm according to the documentation: "You can download a ZIP file from https://npmjs.org/dist/, and unpack it in the same folder where node.exe lives." Make sure if you do this that you get rid of your previous installation first (though overwriting it will probably work ok...).

  • To update your modules, use the npm update command

3
  • npm update npm -g worked for me. I run OSx, and have node installed via homebrew, however, brew upgrade node won't run if you already have the latest node version running, as I did. Piece o' cake. Commented Mar 1, 2015 at 17:09
  • npm install -g npm@latest worked for me on Windows8 stackoverflow.com/a/29023180/588759
    – rofrol
    Commented Apr 11, 2016 at 1:59
  • @rofrol I tried that command, it didn't work for me on Win 8.1. npm still had old version 3.8.0 even though my previous version was 6.10.x. I needed to upgrade npm to at least 6.11.x for Angular. Now I can't seem to get rid of the old version.
    – Chris22
    Commented Sep 27, 2020 at 12:15
31

I think the best way to manage node.js is to use NVM. NVM stands for Node Version Manager.

It's a must-have tool for node.js developers!

You can install NVM using the following command, open terminal and run any one of the following:-

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash

or

wget -qO- https://raw.githubusercontent.com/nvm-sh//nvm/v0.39.0/install.sh | bash

After installing this it's recommended to close the current terminal and open a new one since NVM will be adding some environment variables so terminal needs to be restarted.

I'll list down some of the basic commands for using NVM.

  • This will fetch all the node versions from the internet. All node versions from beginning till date will be shown, It will also mention LTS versions alongside.
nvm ls-remote 
  • This will show you the recommended versions of npm and node you should use. (This works only if you already have a version of npm installed, if not use the above command)
npm doctor
  • This will install the node version which you want (version list is obtained using the above command)
nvm install <version recommended by 'npm doctor' or 'nvm ls-remote'>

for example nvm install v14.18.1

  • This command will give us the list of node versions that are installed locally
nvm ls
  • This command is used to remove the node version that you want from your computer
nvm uninstall v10.15.1
  • The following command will help you upgrade to the latest working npm on the current node version
nvm install-latest-npm
  • NVM can be used to manage multiple node versions simultaneously
  • It can also help you install all the global npm packages from one version to another instead of manually installing each one of them!
  • There are many other uses of nvm the details of which and the commands can be found here Node Version Manager
1
  • 1
    Thanks @lightwing for updating links! :) Commented Dec 11, 2021 at 7:29
26

Try the latest stable version of npm

See what version of npm you're running:

npm -v

Upgrading on *nix (OSX, Linux, etc.)

(You may need to prefix these commands with sudo, especially on Linux, or OS X if you installed Node using its default installer.)

You can upgrade to the latest version of npm using:

npm install -g npm@latest

Or upgrade to the most recent release:

npm install -g npm@next

Upgrading on Windows


By default, npm is installed alongside node in

C:\Program Files (x86)\nodejs

npm's globally installed packages (including, potentially, npm itself) are stored separately in a user-specific directory (which is currently

C:\Users\<username>\AppData\Roaming\npm).

Because the installer puts

C:\Program Files (x86)\nodejs

before

C:\Users\<username>\AppData\Roaming\npm

on your PATH, it will always use the version of npm installed with node instead of the version of npm you installed using npm -g install npm@<version>.

To get around this, you can do one of the following:

  • Option 1: edit your Windows installation's PATH to put %appdata%\npm before %ProgramFiles%\nodejs. Remember that you'll need to restart cmd.exe (and potentially restart Windows) when you make changes to PATH or how npm is installed.

  • Option 2: remove both of

    • %ProgramFiles%\nodejs\npm
    • %ProgramFiles%\nodejs\npm.cmd
  • Option 3: Navigate to %ProgramFiles%\nodejs\node_modules\npm and copy the npmrcfile to another folder or the desktop. Then open cmd.exe and run the following commands:

cd %ProgramFiles%\nodejsnpm install npm@latest

If you installed npm with the node.js installer, after doing one of the previous steps, do the following.

  • Option 1 or 2

    • Go into %ProgramFiles%\nodejs\node_modules\npm and copy the file named npmrc in the new npm folder, which should be %appdata%\npm\node_modules\npm. This will tell the new npm where the global installed packages are.
  • Option 3

    • Copy the npmrc file back into %ProgramFiles%\nodejs\node_modules\npm

A brief note on the built-in Windows configuration

The Node installer installs, directly into the npm folder, a special piece of Windows-specific configuration that tells npm where to install global packages. When npm is used to install itself, it is supposed to copy this special builtin configuration into the new install. There was a bug in some versions of npm that kept this from working, so you may need to go in and fix that up by hand. Run the following command to see where npm will install global packages to verify it is correct.

npm config get prefix -g

If it isn't set to <X>:\Users\<user>\AppData\Roaming\npm, you can run the below command to correct it:

npm config set prefix "${APPDATA}/npm" -g

Incidentally, if you would prefer that packages not be installed to your roaming profile (because you have a quota on your shared network, or it makes logging in or out from a domain sluggish), you can put it in your local app data instead:

npm config set prefix "${LOCALAPPDATA}/npm" -g

...as well as copying %APPDATA%\npm to %LOCALAPPDATA%\npm (and updating your %PATH%, of course).

Everyone who works on npm knows that this process is complicated and fraught, and we're working on making it simpler. Stay tuned.

Source: https://docs.npmjs.com/troubleshooting/try-the-latest-stable-version-of-npm

1
  • if devWL's solution doesn't work, I'm going to try yours. Thanks for the indepth answer. At this point, I'm wondering should I just uninstall Node and NPM -- it has taken me all day to try to fix this just so I can run Angular apps. I installed Node 12.18.4 today and thought it would update npm, but instead, when I run npm -v now, I get 3.8.0 as version when it previously was 6.10.x. Frustrating...
    – Chris22
    Commented Sep 27, 2020 at 13:02
22
$ npm install -g npm stable

Worked for me to update from 1.4.28 to 2.1.5

19

Install npm => sudo apt-get install npm

Install n => sudo npm install n -g

latest version of node => sudo n latest

Specific version of node you can

List available node versions => n ls

Install a specific version => sudo n 4.5.0

17

To install the latest version of npm using npm:

sudo npm install npm@latest

I run this on Linux so I am not sure about other operating systems.

On Linux you can also run:

sudo apt-get update
sudo apt-get upgrade

This will tell the apt-get package manager to update and upgrade all packages.

3
  • For Linux users. To solve "Error: EACCES: permission denied", you can sudo command before running npm install. sudo npm i npm@latest
    – ibnɘꟻ
    Commented May 17, 2020 at 16:18
  • Or you can log in as root if you don't want to use sudo
    – Justin Liu
    Commented May 25, 2020 at 14:57
  • You will typically want to npm install npm -g, right? Commented Oct 17, 2020 at 13:06
16

I just installed Node.js on a new Windows 7 machine, with the following results:

> node -v
v0.12.0
> npm -v
2.5.1

I then did the above described procedure:

> npm install -g npm

and it upgraded to v2.7.3. Except than doing npm -v still gave 2.5.1.

I went to the System configuration panel, advanced settings, environment variables. I saw a PATH variable specific to my user account, in addition to the global Path variable.
The former pointed to new npm: C:\Users\PhiLho\AppData\Roaming\npm
The latter includes the path to node: C:\PrgCmdLine\nodejs\ (Nowadays, I avoid to install stuff in Program Files and derivates. Avoiding spaces in paths, and noisy useless protections is saner...)
If I do which npm.cmd (I have Unix utilities installed...), it points to the one in Node.

Anyway, the fix is simple: I just copied the first path (to npm) just before the path to node in the main, global Path variable, and now it picks up the latest version.
<some stuff before>;C:\Users\PhiLho\AppData\Roaming\npm;C:\PrgCmdLine\nodejs\

> npm -v
2.7.3

Enjoy. :-)

3
  • did you put this as the last entry in your Environment Variables Path?
    – Chris22
    Commented Sep 27, 2020 at 12:36
  • 1
    @Chris22 It is not important, the important part is that the path to npm is before the path to nodejs. And, of course, you must have no other paths to identical names before these!
    – PhiLho
    Commented Oct 1, 2020 at 13:40
  • thanks. I'll keep this in mind. I still don't understand why when I installed node 12.x the other day, my npm version went backwards to 3.8.x when it was at version 6.x.x.
    – Chris22
    Commented Oct 8, 2020 at 16:07
15

For Linux, OSX, etc..

To install the latest version of NPM

npm install -g npm@latest

Or To Install the most recent release

npm install -g npm@next

Additional : To check your npm version

npm -v

If you are in a Windows Machine, I suggest going to the npm website

2
  • What is the difference between the latest and the most recent?
    – Chris Lang
    Commented Sep 13, 2017 at 18:49
  • 1
    @Chris Lang recent is the newest version but the latest is the newest stable version Commented Sep 17, 2017 at 5:29
14

Just run the following command in terminal as root/administrator:

npm i -g n
n stable
npm update -g npm

It has worked for me on Linux

0
14

You could try this

npm install -g npm@latest

14

I recently stumbled across this article: http://martineau.tv/blog/2013/12/more-efficient-grunt-workflows/ and the author mentions npm-check-updates -u && npm install to update all dependencies.

3
  • david-dm.org And this just appeared as a comment from the post I cited. Seems perfect... Commented Jan 30, 2014 at 16:43
  • To be clear, npm-check-updates is another package that needs to be installed.
    – Jens
    Commented Apr 7, 2022 at 4:33
  • The link is broken: "404: NOT_FOUND" Commented Sep 1, 2023 at 14:13
13

You can upgrade to the latest version of npm using:

npm install -g npm@latest
12

To update npm :

npm install npm@{version} -g

to update npm to the latest version:

npm install npm@latest -g

and to check the version :

npm -v

to update node js :

sudo npm cache clean -f
sudo npm install -g n
sudo n stable

to check :

node -v
11

Sometimes it's just simpler to download the latest version from http://nodejs.org/

Especially when all other options fail.

http://nodejs.org/ -> click INSTALL -> you'll have the latest node and npm

Simple!

6
  • 1
    Definitely simpler, but doesn't always work. I've struggled with all of the above suggestion and some more - none worked on my mac. Going to nodejs.org fixed it.
    – guya
    Commented Jun 28, 2014 at 20:51
  • 1
    in that case, you probably installed it in a bad way to begin with. Node/npm is one of those you want to prefix your installation so you don't need sudo for everything. Commented Aug 23, 2014 at 6:59
  • I probably installed it normally using brew or the installer. It might have been a specific bug in a specific npm version. I will probably never know and its probably doesn't matter - it was updating perfectly since than using npm update.
    – guya
    Commented Aug 24, 2014 at 11:02
  • 2
    This worked for me, thanks. I was having issues getting to the latest, and screwed things up when I unknowingly emptied the npm cache, heh. Had to (re)install the msi (on Windows) to get fully updated. Worked perfectly.
    – JasonH
    Commented Dec 1, 2014 at 21:38
  • If you're using OS X or Windows, the best way to install or update Node.js and npm is to use one of the installers from the Node.js download link , when I were downloaded and extracted, it simply updated node and npm version on my macbook pro os x 10.13.
    – Kuhan
    Commented Oct 18, 2017 at 2:37
11

Just listened to an interview with the npm team on the latest episode of nodeup, and they recommended not using update for the update from 1.x to 2.x. Instead, use: npm install npm -g

9

Just with this code

npm install update
9

When it comes to Linux I suggest an Update Node Using a Package Manager:

Node comes with npm pre-installed, but the manager is updated more frequently than Node. Run npm -v to see which version you have, then npm install npm@latest -g to install the newest npm update. Run npm -v again if you want to make sure npm updated correctly.

To update NodeJS, you’ll need npm’s handy n module. Run this code to clear npm’s cache, install n, and install the latest stable version of Node:

sudo npm cache clean -f
sudo npm install -g n
sudo n stable

To install the latest release, use n latest. Alternatively, you can run n #.#.# to get a specific Node version.


When it comes to Windows/ macOS I suggest using Installers on Nodejs.org

The Node.js downloads page includes binary packages for Windows and macOS — but why make your life more difficult? The pre-made installers — .msi for Windows and .pkg for macOS — make the installation process unbelievably efficient and understandable. Download and run the file, and let the installation wizard take care of the rest. With each downloaded update, the newer versions of Node and npm will replace the older version.

Alternatively, macOS users can use the npm and n instructions above.


When it comes to updating your node_modules dependencies folder, I suggest skipping all the things that could cause you a headache and just go to your specific project and re-run npm install again.

Before anyone does that, I suggest first checking your package.json file for the following:

As a user of NodeJS packages, you can specify which kinds of updates your app can accept in the package.json file. For example, if you were starting with a package version 1.0.4, this is how you could specify the allowed update version ranges in three basic ways:

To Allow Patch Releases: 1.0 or 1.0.x or ~1.0.4
To Allow Minor Releases: 1 or 1.x or ^1.0.4
To Allow Major Releases: * or x

Explanation:

MAJOR version for when there are incompatible API changes. --> ~

MINOR version for when functionality is added in a backwards compatible manner. --> ^

PATCH version for when backward compatible bug fixes are done. --> *

8

for nodejs should uninstall it and download your favorite version from nodejs.org for npm run below line in cmd:

npm i npm
8

Warning: if you need update Node from an old version (in my case v4.6.0) it is better to re-install nodejs from scratch (download link: https://nodejs.org) otherwise npm will also update itself to a version that's not compatible with the new Node (see this discussion).

This is the error message that I got after updating Node (on Windows) with npm

$ npm install -g npm stable
[ . . .]
$ npm 
C:\Users\me\AppData\Roaming\npm\node_modules\npm\bin\npm-cli.js:85
      let notifier = require('update-notifier')({pkg})
      ^^^

SyntaxError: Block-scoped declarations (let, const, function, class) not yet supporte
d outside strict mode
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:373:25)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at startup (node.js:139:18)
    at node.js:974:3

After new installation npm works again:

$ npm -v
6.5.0
$ node -v
v10.15.0
0
7

Also if you want to update to a particular version, follow this:

sudo npm cache clean -f
sudo npm install -g n
sudo n <specific version>
1
  • 2
    npm WARN using --force I sure hope you know what you are doing.
    – Jeff
    Commented Apr 14, 2016 at 16:39
7

Just run the below scripts on console:

sudo npm i -g n
sudo n stable
sudo npm update -g npm

This will work for Linux and MAC only

Not the answer you're looking for? Browse other questions tagged or ask your own question.