As a front-end, Node is our required environment. Are we really in the right position when installing Node? Install a Node directly from the official website? It is not recommended to install it directly. There is something called NVM

What is NVM?

NVM is a tool that allows you to install and switch between different versions of Node on the same machine

Why do YOU need NVM?

As front-end developers who are ahead of the curve, Node is constantly being updated, and we’re all following along. You’re having fun with the latest Version of Node (v16.8.0 at the time of writing) when your boss pulls an old project out of the corner and asks you to maintain it. What do you do when you run your project and discover that the old project requires Node version 8.x. x? Would you like to uninstall the latest Node from your computer and install an older version? Color {red}{obviously not} obviously not, this time we need to be able to live in our computer at the same time multiple Node versions, and can be arbitrarily switch tools, NVM shine ✨ debut

2. NVM installation

1. You don’t have to listen

I used to have node_cache confusion and node version switchover problems because I was young and confused. If there are any beautiful boys who failed to uninstall node, please remember to share with me

2. Installation mode

window

Link: github.com/coreybutler… The following versions can be downloaded:

  • Nvm-noinstall. zip: green installation-free version, which requires configuration.
  • Nvm-setup. zip: installation version, recommended

After decompression NVM – setup. Exe to fool a key installation pit: remember the installation directory has a special symbol, Chinese, Spaces, such as: C: ProgramFiles, this directory will not \ color {red} {have pit: remember not have special symbols in the installation directory, Chinese, Spaces, such as: C: ProgramFiles, this directory must not} pit: remember installation directory do not have special symbols, Chinese, space, such as: C:ProgramFiles, this directory must not

mac

The installation of MAC is slightly more troublesome than that of Windows. It is also affected by the version of macOS. I used 11.2.2 here, so if there is a pit in the installation process, you can use it to see if it is affected by the version of macOS

1. Installation commands:

ζ°‘θΏεˆ†ε­

$curl - o - https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bashCopy the code

2. Check the installation

$ nvm --version
Copy the code

3 check the installation of the premise is that you have a successful installation, the fact is often not satisfactory, there will always be pits, you may encounter the following?

1. DNS contamination error was reported, DNS resolution was contaminated in the process of pulling NVM, so that it could not be read normally (of course, there may be other reasons, other reasons are not known yet, if you know, please advise)
curl: (7) Failed to connect to raw.githubusercontent.com port 443: Connection refused
Copy the code

DNS pollution solution:

1. Go toIPAddress.comView the actual IP address

2. curl -o- Raw.githubusercontent.com/nvm-sh/nvm/…The pull of the domain is | bash command
r a w . g i t h u b u s e r c o n t e n t . c o m \color{red}{raw.githubusercontent.com}
, search, and the following results appear

We found four parsed IP addresses, randomly pick out one (of course, unfortunately, you may pick out the one that does not work, you can change another)

3. Modify the host

$ sudo vim /etc/hosts
Copy the code

Add to host

185.199.108.133 raw.githubusercontent.com
Copy the code

When you’re done, try pulling again

2. zsh: command not found: nvm

$NVM –version: ZSH: command not found: NVM – $NVM –version: ZSH: command not found: NVM

Iii. Daily use of NVM

Common commands

  • NVM LS lists all installed versions
  • NVM ls-remote lists all installable versions
  • NVM install Install the specified version, for example, install V4.4.0, NVM install v4.4.0
  • NVM Install Stable Installs the latest stable version of Node
  • NVM uninstall Deletes an installed version. The syntax is similar to install
  • NVM use Switches to using node of the specified version
  • NVM alias Default For example, NVM alias Default v11.1.0
  • NVM –help to view all help commands

Install the sample

1. nvm ls

Let’s use NVM to install a bunch of Nodes, starting with NVM LS to see the current versionTo read the graph briefly, I first see that my computer has a three-node version

, respectively,V8.17.0, v12.18.4, v14.17.4Three versions

The version currently in use is V14.17.4 nvm use <version>Β Switch version

The default version is: v14.17.4 Why does this default version exist? through nvm use <version>Β The switch version is only temporary. After you shut down the terminal, the default version will be restored. If you want to switch the specified version for a long time, set this default version

NVM alias default <version> For example, NVM alias default v11.1.0Copy the code

2. NVM ls-remote If we don’t know which version to install for a while, let’s get one

nvm ls-remote 
Copy the code

NVM install vxx.xx.xx, NVM ls install vxx.xx.xx, NVM ls install vxx.xx.xx

NVM install v11.1.0 NVM lsCopy the code

If we want to use vxx.xx.xx for a short term project, just NVM use vxx.xx.xx. If we want to use vxx.xx.xx for a long term, remember to set the default version

NVM alias default <version> For example, NVM alias default v11.1.0Copy the code

Four, pit record

Global dependency package installed cannot be found?

Node_modules: Node_modules: node_modules: node_modules: node_modules: node_modules: node_modules NRM (NPM Registry Manager) is the mirror source management tool of NPM. Sometimes foreign resources are too slow. You can use this tool to quickly switch between NPM sources. I used to use the node 12 version, but I switched to the node 14 version and installed an NRM. When EXECUTING the NRM, I found an error, indicating that there is no dependency. We can get through

NPM config ls // Check the NPM configurationCopy the code

If the global dependency installation fails after switching node versions, check the NPM prefix configuration to see if it is the node version you are currently using

Node_modules: Node_modules: node_modules: node_modules: node_modules: node_modules: node_modules: node_modules: node_modules: node_modules: node_modules: node_modules: node_modules: node_modules: node_modules We need to change the cache and prefix corresponding versions to the ones we want

NPM config set prefix "D:\ProgramFiles\ NVM \v version \node_cache" NPM config set cache "D: \ ProgramFiles \ NVM \ v12.12.0 \ node_cache"Copy the code