In daily front-end or node back-end development, some of the node dependencies used may not support the currently used node version. At this time, if you want to use this dependency for happy development, you can only switch the node version.
However, although the trouble of uninstalling and installing node is acceptable, if you want to use the syntax support of the higher version of node in other projects, you must use webpack or gulp to build and compile tools, or you still need to use the higher version of node.
At this time, nvm appeared.
Nvm is the node version manager. You can use it to install multiple versions of node, and then you can easily switch the currently used node version with one line of command.
In other words, you don't need to uninstall and install anymore, just use nvm to install which version you want to use, and just switch with one instruction after installation!
The following is an explanation of some steps based on the mac system.
Install
Uninstall the globally installed node package
Now that you use nvm'hosting', you need to uninstall the global node package you installed before to prevent problems.
npm, cnpm, yarn, etc. will all be deleted and will need to be reinstalled later.
The following command, you can view the module node function modules that have been installed globally, so that you can delete these global modules and then reinstall them globally according to different node versionsnpm ls -g --depth=0
Below are the uninstall steps
You can also use type -a node
check the path, the location may be different for different machines and different installation methods.
- Delete the global node_modules directory, there are actually only a few folders inside, which are installed globally by npm and using npm, such as yarn, cnpm, etc.
sudo rm -rf /usr/local/lib/node_modules
- Deleting node is actually uninstalling the node environment
sudo rm /usr/local/bin/node
- Delete the soft link registered by the global node module. If you understand the linux command syntax, you can probably understand it. The command below is to enter the system's global command file first, and filter out all the commands that depend on node_modules (in fact, it is still step 1 Those instructions in), and then delete
cd /usr/local/bin && ls -l | grep "../lib/node_modules/" | awk '{print $9}'| xargs rm
You can use the terminal to input node -v
, if it prompts node: command not found
, it means that the uninstallation is complete
Install nvm
There are many ways to install nvm, the following are two ways, it is recommended to check the latest installation statement on the official website (due to external network problems, the installation is likely to fail, there are other installation methods later):
curl method:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
wget method:
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
The above two statements will execute the nvm installation script. After the installation is complete, there will be a print similar to the following:
=> Profile not found. Tried (as defined in $PROFILE), ~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile.
=> Create one of them and run this script again
=> Create it (touch ) and run this script again
OR
=> Append the following lines to the correct file yourself:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
=> Close and reopen your terminal to start using nvm or run the following to use it now:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
This means that we need ~/.bashrc
, ~/.bash_profile
, ~/.zshrc
, ~/.profile
, save and close, and then restart the terminal to use the nvm command.
v0.35.3
version are as follows, you can install the latest version of nvm directly on the official website and paste the latest version of the instructions (the following are the instructions provided by the official website, or you can use the instructions printed above after the installation is complete):
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
It is recommended to create ~/.bash_profile
, if there is, add the above code directly at the end of the inside, then execute source ~/.bash_profile
, reload this file, nvm can be used normally.
installation failed
The above statement may cause the installation to fail due to network problems.
At this point, you can enter the nvm official tutorial (actually a github page), here will provide a variety of installation methods (but in the end it seems that we need to clone the nvm warehouse, we can manually clone the warehouse).
but , github may not be able to open, then we also open the domestic "githup" code cloud search nvm-sh , find the project cloned here from github by passionate netizens, and use it the same.
The following is a general explanation of the simpler one:
Git Install:
Go to the ~ directory and use git to clone the nvm repository:
cd ~/
git clone https://github.com/nvm-sh/nvm.git .nvm
Enter, switch to the latest version branch (the actual latest branch, you can go to the official website to have a look):
cd .nvm
git checkout v0.38.0
Execute the installation instruction, this instruction is executed instantly:
. ./nvm.sh
Finally, click any one of several files, ~/.bashrc
, ~/.profile
, or ~/.zshrc
, and paste in the following code:
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
Finally, let the system re-read this file, for example, put this into the ~/.bashrc
file, then execute:
source ~/.bashrc
Finished, nvm can be used at this time.
nvm use
Mainly the following commands:
Function | instruction |
---|---|
List currently installed node and installable lts (long-term support stable version) version | nvm ls |
List the versions of all remote servers | nvm ls-remote |
Install the specified version, it can be installed fuzzy, it is recommended to query first and then install, such as nvm install v11.11.0 | nvm install <version number> |
Install the latest stable version of node | nvm install stable |
Delete the specified version that has been installed, the syntax is similar to install | nvm uninstall <version number> |
Set a default version, the version used by the newly opened terminal | nvm alias default <version number> |
Add aliases to different version numbers | nvm alias <alias> <version number> |
Cancel an alias | nvm unalias <alias> |
Switch the version used by the current terminal, but it only takes effect for the current terminal and does not affect other | nvm use <version number or alias> |
Show current version | nvm current |
Delete the defined alias | nvm unalias <alias> |
In the current version of node environment, reinstall the npm package of the specified version number globally | nvm reinstall-packages <version number> |
Use the terminal to enter a different directory, avn automatically switches the node version
This function was not originally needed, but in recent years it has been discovered that when there are too many businesses and projects, it may be really needed.
For example, if you receive a project, you may use a variety of node versions; for projects developed by your company, you use a fixed node version; occasionally you may research some new technologies yourself, and you may use the latest version.
nvm use to switch when I entered a project, but in general it is very troublesome. You have to do this every time you open the terminal.
I searched the Internet and found this tool: avn .
First, open a new terminal to ensure that you are using the default version of node. This is because the global dependencies installed under different node versions in nvm are not interoperable.
This tool is based on node. We only need to install it globally under node by default. Whenever you use the terminal to enter a different directory, node defaults to the version, and then it will automatically execute and switch to the node version you want to specify next.
After opening the terminal, first install it globally:
npm install -g avn avn-nvm avn-n
Then start this tool:
avn setup
Note: I don’t know why, I can start using the dependencies installed by node^11.11.0. Instead, I use node^12.21.0. It is still a long-term support version. The dependencies installed using this version will start up. Error if (cb) cb.apply(this, arguments) is strange, but you can install dependencies and start under v11.11.0, and then switch node version will not affect it, if it affects, it will be in other Dependencies are also installed under the version.
Next, go to the project where you need to customize the node version, create the file -version , and write the node version you need directly, such as:
This node version needs to be installed with nvm, otherwise the error avn could not activate node v14.15.0 will be reported.
v14.16.0
Then, restart the terminal under this item, and a prompt will appear:
avn activated v14.16.0 via ../.node-version (avn-nvm v14.16.0)
At this time, the node used by this terminal is v14.16.0.
optimization: But the above method requires adding this file to each custom node version project, which is very unfriendly.
In fact, we can put this file in the parent directory of this project, and it will also take effect, so that all projects that use the same node version are placed in this folder, which is very friendly.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。