DEV Community

Cover image for Easily upgrade or downgrade your Node.js version
Arifa
Arifa

Posted on

Easily upgrade or downgrade your Node.js version

Sometimes, when you are working on a project which uses a different version of Node.js than what's installed on your dev machine, you may want to change to the Node.js version required by the project.

All you have to do is install a package called n, which caches node versions on your dev machine.

Install n using npm

npm install -g n
Enter fullscreen mode Exit fullscreen mode

After executing the above command, you may receive a series of npm warnings followed by npm ERR! You can execute these three commands which are also mentioned here.

# make cache folder (if missing) and take ownership
sudo mkdir -p /usr/local/n
sudo chown -R $(whoami) /usr/local/n
# take ownership of node install destination folders
sudo chown -R $(whoami) /usr/local/bin /usr/local/lib /usr/local/include /usr/local/share
Enter fullscreen mode Exit fullscreen mode

Now, you can install any version of Node.js you want.
Executing as n <version> you can download and install a version of Node.js. If the <version> has already been downloaded, n will install from its cache.

Alt Text

To change to the latest version of node

Alt Text

You can also check the versions of node your n package has, by simply executing the following command n

Alt Text

Executing the above command provides the versions cached.

Alt Text

I hope this post helps you switch between different Node.js versions. Please like this post and follow me for more such posts.

References
[1] https://www.npmjs.com/package/n

Top comments (1)

Collapse
 
jjaimealeman profile image
Jaime Aleman

Thanks for this!
I have node --version 17.3.0 and tried to install Strapi, but it's not compatible with the newest version (yet), so I had to downgrade to 16.13.2 - and it works perfectly!