NVM: Manage Multiple Node.js Versions
For now, more and more tools are depended on Node.js. Such as apps which based on Electron, some scaffold of frameworks or languages, and so on. So, there are too much reasons to ask us to install Node.js.
To install it, there are many ways. Such as downloading installer from official site, use package manager, thirdparty tools like HomeBrew, and so on.
I recommend using NVM to install and manage the Node.js. With NVM, you can install multiple versions of Node.js at same time on your machine, and switch or remove them at anytime.
This post will tell you how to do it.
Install NVM
Firstly, we need install NVM to our machine.
Open NVM Repo on Github page and scroll down to ‘Installing and Updating’ section of README. Choose any one of installation way between ‘curl’ or ‘wget’, and copy the command to your terminal, then press ‘Enter’.
For example, I like to use ‘curl’, so, I copy following command to my terminal and press ‘Enter’ to install.
1 | $ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash |
After installation succeed, the NVM is installed into ‘.nvm’ folder under your user home. And the installer added following settings to your .bashrc (for macOS Catalina, it should be .zshrc)
1 | export NVM_DIR="$HOME/.nvm" |
close your terminal and reopen it, or, just execute following command in your current terminal
1 | $ . ~/.bashrc |
or
1 | $ source ~/.bashrc |
Then use following command to check status of installation
1 | $ command -v nvm |
If the installation is correct, it should show
1 | nvm |
If not shown, please check the output of installer to find the issues.
Install Node.js
After it, we need find a verson of Node.js to install. Run following command, it will show all versions which can be installed.
1 | $ nvm ls-remote |
You will see lots of versions shown in terminal, usually, we always choose one of LTS version to use. So, above command also can be following to only show LTS versions.
1 | $ nvm ls-remote --lts |
After we confirmed which version should be installed, we can start to install it with a simple command. For example, we choose the ‘v12.16.3 (Latest LTS: Erbium)’ version to install.
1 | $ nvm install v12.16.3 |
When it done, you can use following command to check the state of Nodejs
1 | $ nvm ls |
It will show the details what you installed. Such as, following information will be shown on my machine
1 | -> v12.16.3 |
Install multiple versions and manage them
If you need install multiple versions of Node.js to fit you requirements, you can repeat Install Node.js section until all versions which you want are installed.
Set current used version
Before you using Node.js, may you want to check which version is current used
1 | $ nvm ls |
It will show
1 | -> v10.20.1 |
With it, you can easy to find the current used version is v10.20.1, and there also has v12.16.3 can be used.
If you need to use v12.16.3 now, you can change it by
1 | $ nvm use 12.16.3 |
Then run ‘nvm ls’ command and you will find current used version is changed to v12.16.3.
1 | v10.20.1 |
Use alias
You can use ‘default’ to replace the version name which you almost use. For example, the verson v12.16.3 is mine.
1 | $ nvm alias default v12.16.3 |
If succeed, it will show
1 | default -> v12.16.3 |
It is useful, with alias you can mark each version for different usages. For example, I use version 10.20.1 for working, and version 12.16.3 for studying. I just run
1 | $ nvm alias work v10.20.1 |
Then, we just need use the alias to switch for my current usage but need not remember the version.
1 | $ nvm use work |
and also, you can unmark it with
1 | $ nvm unalias <alia-name> |
Uninstall a version of Node.js
If there is a version of Node.js which you will not use, you can remove it simply.
1 | $ nvm uninstall <version> |
Upgrade
To upgrade the nvm, we also need open the NVM official site and copy the script and run it in terminal. This way is recommended.
And also, you can update manually with following command
1 | $ ( |
OK, above are usages which I used, for other functions, I will update this post when I use them.
Conclusion
I hope this post can give you help. If you have any questions or suggestions, please feel free to add your comments to Issue