How to install specific npm and node versions on Linux
Javascript seems to have taken over the web in recent years, and if you run self-hosted software on your Linux server, there's a good chance that at some point you're doing to have to deal with node.js and node package manager (npm).
A lot of node.js software projects were written with, and for, a specific version of node.js, and using a newer or older version could result in unexpected behaviour, or the software may not run or install at all.
In this case, you'll need to upgrade or downgrade node.js and npm to specific versions to match the projects requirements. Here's how to do it.
What is node anyway?
For most people, the daily experience of JavaScript is in a web browser. It's a sort of programming language that allows individual pages to perform complex programmatic functions in a way that simple HTML can't. If the page you're looking at has a form, or any kind of interactive element, the chances are good that it's JavaScript behind the scenes.
But JavaScript doesn't just run in your browser. It can run on the back-end as well. Node.js is a runtime environment that allows you to run JavaScript code outside of a server or even just on your PC. It's great for self-hosted apps that run JavaScript code both on the server-side and the client-side.
Why do you need npm?
Node Package Manager (npm), as the name suggests, is a package manager for node.js. In simple terms, it's like an app store for JavaScript, where you can find and install reusable pieces of code to use in your ownprojects. npm makes it simple for developers to add functionality to a project by providing access to a vast ecosystem of open source packages created by other people.
If you're a dedicated self-hoster (as we are), you probably didn't write the code yourself, and are instead installing some awesome web service to play around with and improve your life.
And for some projects you'll need very specific versions of both node.js and npm.
Find out what versions of node and npm you have installed
If you've installed nope and npm from your distros package manager, the chances are good that what's installed on your system is far behind the most recent versions of npm and node.
And if you're installing node-based software that was written some time ago, then you'll most likely need to downgrade npm and node in order to prevent problems.
Consult the project documentation to see what version of node and npm you require, then pop open a terminal, and enter:
node --version
then:
npm -version
Each command will return the version of the software you have installed.
As you can see from the screenshot, the node version on this writer's system is v18.16.0, while our node package manager is at 9.5.1.
Change your node.js version with n
There is a node package simply called n, that you can use to easily install, switch, and manage different versions of Node.js on your system.
Unfortunately, n doesn't come preinstalled, and it shouldn't come as a surprise that we recommend using npm to install n as well.
To install n, open a terminal and enter:
npm install -g n
With n installed you can change to a rquired node version by adding it as an argument to the n command. For instance, to install and switch to node version 14, you would enter:
n 14
It's that simple
Use npm to change npm version
As npm is both a package manager and a package, you can use npm to install different versions of itself quite easily.
We're currently on version 9.5.1 of npm, and we want to downgrade to version 6 in order to instal Habitica - a gamified productivity and task management app.
To change you npm version, open your terminal, and enter:
npm install -g npm@6.1.0
In case you're interested, the g switch is for "global", and means that npm version 6 will be installed globally on your system, rather than locally within the current directory.
Now run:
npm -version
...to check the current npm version.
Obviously, you're not going to want to install npm version 6 in every situation, and you should check available npm versions to see which one you want.