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
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
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.
To change to the latest version of node
You can also check the versions of node your n package has, by simply executing the following command n
Executing the above command provides the versions cached.
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)
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!