How to Upgrade (or Downgrade) Node.js Using npm
By Cory LaViska on May 29, 2019
Need to update your version of Node.js? Here’s how you can upgrade or downgrade from the command line using npm.
As you may know, Surreal CMS used to be a PHP app but last year I rebuilt it in Vue.js + Node.js. Being able to switch between Node versions quickly was really helpful while developing, so here are my notes for managing which version is currently installed.
Big news! 🎉
I just made Surreal CMS free for personal, educational, and non-profit websites. If you’re looking for a new way to manage your static website, check it out and let me know what you think!
Determining your Node version
First off, let’s figure out which version of Node is currently installed.
node -v
The version number displayed is the one that’s currently active on your machine. Now, let’s install a program called n that will let us easily switch between Node versions.
sudo npm install -g n
Upgrading to the latest stable version
Once n is installed, this simple command will update you to the latest stable version of Node.
sudo n stable
Changing to a specific version
If you need a specific version, simply specify the version number you want like this.
sudo n 10.16.0
You can find a full list of releases here.
As you can see, n is a really handy program that makes switching between Node versions effortless. It’s worth keeping around if you do any serious development with Node.js.
Other way for upgrade or downgrade nodeJs is NVM:
How to Install NVM on Linux and Mac
Since Linux and Mac have some similarities (they are both UNIX-based OSes), you can install nvm on them in similar ways.
1. Run the nvm installer
In your terminal, run the nvm installer like this:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
# orwget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
You can use curl
or bash
depending on the command available on your device.
These commands will clone the nvm repository to a ~/.nvm
directory on your device.
2. Update your profile configuration
The installation process from step 1 should also automatically add the nvm configuration to your profile. If you’re using zsh, that would be ~/.zshrc
. If you're using bash, that would be ~/.bash_profile
...or some other profile.
If it doesn’t automatically add nvm configuration, you can add it yourself to your profile file:
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
This command above loads nvm for use.
3. Reload the shell configuration
With your profile configuration updated, now you will reload the configuration for your terminal to use:
source ~/.bashrc
With this command executed, nvm is ready for you to use. You can confirm that nvm is installed correctly by running:
nvm -v
This should show the version of nvm installed.
Wrapping up
With nvm installed, you can now install, uninstall, and switch between different Node versions in your Windows, Linux, or Mac device.
You can install Node versions like this:
nvm install latest
This command will install the last version of Node:
nvm install vX.Y.Z
This will install the X.Y.Z
Node version.
You can also make a version your default by running:
nvm alias default vX.Y.Z
And if you want to use a specific version at any point, you can run the following in your terminal:
nvm use vA.B.C
NVM makes it easier to manage multiple versions of Node.js across different projects that require different versions.