433

Is it possible to upgrade node right in place, instead of manually installing the latest stable version?

I have installed node.js version 5.0 with nvm, but now I want to update it to 5.4. I'm trying to avoid having to manually reinstall all of my global packages (e.g. by running npm install -g grunt-cli bower yo yoman-angular-generator blabla blablablabla...).

blizzrdof77
  • 363
  • 4
  • 14
Boris Burkov
  • 13,420
  • 17
  • 74
  • 109

11 Answers11

583

This may work:

nvm install NEW_VERSION --reinstall-packages-from=OLD_VERSION

For example:

nvm install 6.7 --reinstall-packages-from=6.4

then, if you want, you can delete your previous version with:

nvm uninstall OLD_VERSION

Where, in your case, NEW_VERSION = 5.4 OLD_VERSION = 5.0

Alternatively, try:

nvm install stable --reinstall-packages-from=current
Himanshu Tanwar
  • 198
  • 1
  • 11
gabrielperales
  • 6,935
  • 1
  • 20
  • 19
  • 1
    dose using `nvm install stable` remove all the packages installed including installed node rather updating them? – Syed Jul 25 '16 at 06:53
  • 1
    actually, this is no longer working. There is an issue with npm: https://github.com/creationix/nvm/issues/811 – jhuesos Nov 01 '16 at 13:59
  • 8
    By `nvm ls-remote` you can see all releases. – Daniel Jan 19 '19 at 12:59
  • 28
    This works for me: `nvm install node --reinstall-packages-from=$(nvm current)`. That'll update Node.js to latest version and reinstall npm global packages from whatever the previous version was. – cyfrost May 12 '19 at 08:54
  • If you fail to uninstall, use `nvm deactivate` and try again. – izilotti Apr 14 '20 at 17:31
333

You can more simply run one of the following commands:

Latest version:

nvm install node --reinstall-packages-from=node

Stable (LTS) version: (if currently in use)

nvm install "lts/*" --reinstall-packages-from="$(nvm current)"

This will install the appropriate version and reinstall all packages from the currently used node version.

This saves you from manually handling the specific versions.


Kudos to @m4js7er for commenting about the LTS version.

hakre
  • 193,403
  • 52
  • 435
  • 836
Elad
  • 19,079
  • 18
  • 62
  • 71
  • 27
    If you want to update to latest stable version (lts - recommended for most users), then you should run: `nvm install lts/* --reinstall-packages-from=node`. After that you can cleanup your versions with `nvm uninstall [old version]`. You can list all installed versions with `nvm ls`. – m4js7er Jan 08 '17 at 09:56
  • Double think before doing `--reinstall-packages-from=node` You can use different global environments for versions 6, 8 and 10. – contributorpw Oct 15 '18 at 06:13
  • 3
    I tied the command `nvm install lts/* --reinstall-packages-from=node` but it gave me error saying `Version 'lts/*' not found - try 'nvm ls-remote' to browse available versions.` I am using NVM version 0.30.1, maybe my NVM is too old. I end up manually replacing `lts/*` with `10.15.2/*` to get it to work. – Zhang Mar 02 '19 at 14:55
  • This is the best and easiest command to use to always stay updated.. – Sojimaxi Sep 08 '20 at 08:41
  • 9
    If you're on OSX with the default zsh shell and get `zsh: no matches found: lts/*` simply quote the lts/* argument to prevent Z shell from interpreting the * as a globbing wildcard: `nvm install 'lts/*' --reinstall-packages-from=node` – tenni Feb 28 '21 at 22:50
  • I was running the current version but, wanted to update the lts version. I wasn't sure if using the flag with a value of node would pull the packages from lts or from current, so I did this instead `nvm install "lts/*" --reinstall-packages-from=14.16.1` Of course, I could have just switched to the old lts but, thought I'd share, in case it was helpful for others. – Natetronn Oct 02 '21 at 02:24
  • When using fish -> `nvm install "lts/*" --reinstall-packages-from=(nvm current)` – MADforFUNandHappy Nov 16 '21 at 09:39
55

TWO Simple Solutions:

To install the latest version of node and reinstall the old version packages just run the following command.

nvm install node --reinstall-packages-from=node

To install the latest lts (long term support) version of node and reinstall the old version packages just run the following command.

nvm install --lts /* --reinstall-packages-from=node

Here's a GIF animation to support this answer:

nvm

hakre
  • 193,403
  • 52
  • 435
  • 836
Ahmad Awais
  • 33,440
  • 5
  • 74
  • 56
  • 1
    Can't get this to work - I just receive `If --reinstall-packages-from is provided, it must point to an installed version of node.` – wickywills Jan 15 '19 at 11:16
  • @wickywills "node: this installs the latest version of node" maybe you had and LTS or specific version installed and thus `node` did not point to latest version installed. Or you need to update nvm. – illnr Apr 05 '19 at 15:00
  • This worked for me but how can I delete the old node after the new one has been installed and activated? – Zoltan King Dec 27 '20 at 16:46
  • I know this doesn't exactly answer your question but I've stopped using `nvm` in the favor of an extremely fast and low-profile script called `n`. I made a 10-minute video on it — talking about why I moved to `n` and how you can use it. The video is available at https://nodecli.com/nodejs-install-n – Ahmad Awais Jan 03 '21 at 11:41
  • Isn't it duplicating the existing answer? And the gif animation was distracting for me to read the content, I took the liberty to put it into a spoiler so it becomes available after click so that it is easier to read the content on this page. Hope this is helpful. – hakre Oct 05 '21 at 13:12
10

if you have 4.2 and want to install 5.0.0 then

nvm install v5.0.0 --reinstall-packages-from=4.2

the answer of gabrielperales is right except that he missed the "=" sign at the end. if you don't put the "=" sign then new node version will be installed but the packages won't be installed.

source: sitepoint

10

Here are the steps that worked for me for Ubuntu OS and using nvm

Go to nodejs website and get the last LTS version (for example the version will be: x.y.z)

nvm install x.y.z
# In my case current version is: 14.15.4 (and had 14.15.3)

After that, execute nvm list and you will get list of node versions installed by nvm.

Now you need to switch to the default last installed one by executing:

nvm alias default x.y.z

List again or run nvm --version to check: enter image description here

Update: sometimes even if i go over the steps above it doesn't work, so what i did was removing the symbolic links in /usr/local/bin

cd /usr/local/bin
sudo rm node npm npx

And relink:

sudo ln -s $(which node) /usr/local/bin/nodesudo && ln -s $(which npm) /usr/local/bin/npmsudo && ln -s $(which npx) /usr/local/bin/npx
Mostav
  • 2,203
  • 15
  • 27
6

Node.JS to install a new version.

Step 1 : NVM Install

npm i -g nvm

Step 2 : NODE Newest version install

nvm install *.*.*(NodeVersion)

Step 3 : Selected Node Version

nvm use *.*.*(NodeVersion)

Finish

Serkan
  • 142
  • 1
  • 5
  • Currently when you install nvm using npm, you get the response `npm WARN deprecated nvm@0.0.4: This is NOT the correct nvm. Visit http://nvm.sh and use the curl command to install it.` – Mad Bernard Oct 02 '20 at 19:03
4

Here's the steps to upgrade NodeJs version:

  1. Run nvm install node (will install latest version). Alternatively, you can specify a specific version by running nvm install <node_version>.
  2. Run nvm use <node_version> to use it.
  3. If you want to make it the default version on your machine, run nvm alias default <node_version>.

Additional notes:

To find out what node versions you have on your machine and which one is set as your default one, use nvm list command.

Hani
  • 753
  • 8
  • 10
3

For Windows 11 this worked for me on cmd, used with admin rights:

Prerequisite, in case you just installed NVM, is to open a new cmd window after nvm installation.

See installation instructions here: https://github.com/coreybutler/nvm-windows

  1. Get installed versions, using
nvm list
  1. Get current version
nvm current
  1. Install latest version
nvm install latest
  1. Check installed versions to see for newer version, again using
nvm list
  1. Set current version to the latest (cmd with admin rights), you just installed in the previous step
nvm use PUT_VERSION_NUMBER_TO_BE_USED

You can check again if the change was successful using

nvm list
  1. Remove old version, if no longer needed
nvm remove PUT_VERSION_NUMBER_TO_BE_REMOVED

If you want to use the LTS version, install using

nvm install lts
frankfurt-laravel
  • 3,731
  • 2
  • 20
  • 29
1

Bash alias for updating current active version:

alias nodeupdate='nvm install $(nvm current | sed -rn "s/v([[:digit:]]+).*/\1/p") --reinstall-packages-from=$(nvm current)'

The part sed -rn "s/v([[:digit:]]+).*/\1/p" transforms output from nvm current so that only a major version of node is returned, i.e.: v13.5.0 -> 13.

MrSegFaulty
  • 535
  • 6
  • 10
0

I simply run: nvm install 18.17.0 and it worked

0

My steps for upgrading node from v16.20.0 to v18.16.0 are as follows

  • Run nvm list
==> nvm list
->     v16.20.0
       v18.16.0
default -> 16.20.0 (-> v16.20.0)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v18.16.0) (default)
stable -> 18.16 (-> v18.16.0) (default)
lts/* -> lts/hydrogen (-> v18.16.0)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.12 (-> N/A)
lts/fermium -> v14.21.3 (-> N/A)
lts/gallium -> v16.20.0
lts/hydrogen -> v18.16.0
  • Run nvm current
==> nvm current
v16.20.0
  • Run nvm install 18.16.0
==> nvm install 18.16.0
v18.16.0 is already installed.
Now using node v18.16.0 (npm v9.5.1)
  • Run nvm current
==> nvm current
v18.16.0
Arunabh Das
  • 13,212
  • 21
  • 86
  • 109