Update Node.js and NPM via commandline

How to update NodeJS and NPM via ShellToday’s article falls into the category “reminder”. Every few months I write smaller projects with Node.js. But before I start, I’d like to update to the latest version, and that’s where the dilemma starts. Every time I forget the command line commands for it.

Therefore, here are the short and concise CMD/Shell/Bash commands for updating Node.js and NPM. Once for macOS (OSX) / Linux and once for Windows.

Update NPM on Linux and macOS (OS X)

sudo npm install -g npm

Whether the update was successful can be tested with the following command.

npm -v

Update NPM on Windows

The following commands have to be set in Powershell (Start -> “powershell” -> right click -> Run as Administrator). Each line corresponds to one command.

Set-ExecutionPolicy Unrestricted -Scope CurrentUser -Force
npm install -g npm-windows-upgrade
npm-windows-upgrade

Whether the update was successful can be tested with the following command.

npm -v

Update Node.js on Linux and macOS (OS X)

Each line is to be issued as a single command:

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

Whether the update was successful can be tested with the following command.

node -v

Update Node.js on Windows

The following commands have to be set in Powershell (Start -> “powershell” -> right click -> Run as Administrator). Each line corresponds to one command.

Install-Module -Name power-nvm
Add-Type -AssemblyName System.IO.Compression.FileSystem
nvm install latest
nvm default latest

Alternatively, only the Node.js binary can be exchanged via WGET. Should you use the 32-bit (x86) variant, this is done with the following command:

wget https://nodejs.org/download/release/latest/win-x86/node.exe -OutFile 'C:\Program Files (x86)\nodejs\node.exe'

For the 64-bit (x64) variant, the following command must be used:

wget https://nodejs.org/download/release/latest/win-x64/node.exe -OutFile 'C:\Program Files\nodejs\node.exe'

Whether the update was successful can be tested with the following command.

node -v

If the Powershell script shown above does not work anymore, Node.js can also be updated “manually”. All you have to do is download the latest version from the Node.js page and then install it over the current version.

Leave a comment

Please be polite. We appreciate that. Your email address will not be published and required fields are marked