About nvm
nvm is a version manager for node.js, designed to be installed per-user, and invoked per-shell. nvm works on any POSIX-compliant shell (sh, dash, ksh, zsh, bash), in particular on these platforms: unix, macOS, and windows WSL.
nvm allows you to quickly install and use different versions of node via the command line.
$ nvm use 16
Now using node v16.9.1 (npm v7.21.1)
$ node -v
v16.9.1
$ nvm use 14
Now using node v14.18.0 (npm v6.14.15)
$ node -v
v14.18.0
$ nvm install 12
Now using node v12.22.6 (npm v6.14.5)
$ node -v
v12.22.6
Git Install
The most convenient way to install is via the offered script:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
But this method will fail if you have a poor network condition. Of course, there is a way to resolve it, but this is another story.
Let's focus on the git clone way.
If you have git installed (requires git v1.7.10+):
- clone this repo in the root of your user profile
cd ~/from anywhere thengit clone https://github.com/nvm-sh/nvm.git .nvm.If there are some errors, pls refer to # How to resolve Error 443 or 60 when clone repositories via https protocol?
cd ~/.nvmand check out the latest version withgit checkout v0.39.0git checkout v0.39.0is to switch to thev0.39.9tag which points directly to a specific commit in a repository's history.
- activate nvm by sourcing it from your shell:
. ./nvm.sh- Now add these lines to your
~/.bashrc,~/.profile, or~/.zshrcfile to have it automatically sourced upon login: (you may have to add to more than one of the above files)export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion .is the synonym ofsource.-sis a bash operator which checks the operand exists and is a regular file.
- Now add these lines to your