In actual work, nvm is used to manage node packages. Here are some records for your reference.
1. Uninstall existing node and node modules before installation (clean up the environment)
npm ls -g --depth=0 # View globally installed modules to delete these global modules and then reinstall them globally with different node versions
sudo rm -rf /usr/local/lib/node_modules # Delete the global node_modules directory
sudo rm /usr/local/bin/node # Delete node
cd /usr/local/bin && ls -l | grep "../lib/node_modules/" | awk '{print $9}'| xargs rm # Delete the soft links registered by global node modules
2. Install nvm (the most important part)
Install nvm locally using the gitee mirror
echo $SHELL
#/bin/zsh # If it is zsh, change "bash" at the end to "zsh"
# Method 1:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
# Method 2:
git clone https://gitee.com/mirrors/nvm.git ~/.nvm && cd ~/.nvm && git checkout `git describe --abbrev=0 --tags` # You can see that the address is from gitee
3. Configure nvm environment variables
After step 2, nvm is temporarily unavailable. When the terminal is closed and reopened, nvm is still not available. You need to add nvm environment variables. Enter the .bash_profile
file to set the environment variables. If this file does not exist,
vi ~/.bash_profile # Enter (i to edit, esc to exit, :wq to save)
Copy and paste the following two lines and save
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
Save and exit, then execute the command to take effect
source ~/.bash_profile
If you are using the zsh
environment (like me and most of my colleagues), you also need to separately set the .zshrc
file.
# In the zshrc file
...
source ~/.bash_profile # Place it somewhere
Save and exit, then execute the command to take effect
source ~/.zshrc
...
4. Check the installation
After step 3, whether it is a new command line window or the current one, you can check the installation status of nvm with command -v nvm
.
Here are some examples
Install multiple versions of node/npm
For example, if we want to install version 4.2.2, we can use the following command:
nvm install 4.2.2
You can use the following command to list all available versions on the remote server:
nvm ls-remote
For Windows:
nvm ls available
Switch between different versions
Whenever we install a new version of Node, the global environment will automatically set this new version as the default.
nvm provides the nvm use command. The usage of this command is similar to the install command.
For example, switch to 4.2.2:
nvm use 4.2.2
Switch to the latest 4.2.x:
nvm use 4.2
Switch to the latest version:
nvm use node
List installed instances
nvm ls
You can change the default version with the following command: nvm alias default v4.3.0 This way, you don't have to switch versions every time.