banner
leoking

leoking

前端开发者
tg_channel

Installing and using nvm on Mac.

In actual work, nvm is used to manage node packages. Here are some records for everyone's reference.

1. Uninstall existing node and node modules before installation (cleaning 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 using 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 completing Step 2, nvm is still temporarily unavailable. When the terminal is closed and reopened, nvm will be invalid and still not usable. You need to add nvm environment variables by entering the .bash_profile file. 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.

# Inside 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 completing Step 3, at this point, whether it is a new command line window or the current one, you can check the installation of nvm! 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 list all available versions on the remote server with the following command:
nvm ls-remote

For Windows, it is:
nvm ls available

Switching 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.
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.