Home > Article > Web Front-end > A brief discussion on how NVM installs and manages multiple Node versions
This article will introduce to you how to use NVM to easily install and manage multiple versions Node.js. I hope it will be helpful to you!
When we build some projects, we often need older versions of node. For example: Hexo’s currently better supported version is v12, and the latest stable version of Node is v14. At this time, in order to avoid bugs, we need to switch the Node version. [Recommended learning: "nodejs Tutorial"]
This tutorial is applicable to:
Warning⚠️: It is highly not recommended to use the n module under NPM to manage node versions
Hey, it’s not easy to make a tutorial video (Bilibili will not recommend it at all to attract traffic), please be sure toOne click three timesOuch~
Bilibili video address: https://www.bilibili.com/video/BV12h411z7Kq
For different operating systems, we prepare different NVM tools. The following is the project address If you are interested, you can go to the project source address:
For Mac/Linux: https://github.com/nvm-sh/nvm
For Windows: https://github.com/coreybutler/nvm-windows
Before configuring, please uninstall the Node version you have installed and the configured environment variables in advance. avoid confict.
Configure NVM under Windows. According to the configuration method of NVM project address, there are two methods:
The reason why it is not recommended to use the installer to install NVM is that it is inconvenient to manage. There is essentially no difference between the two methods.
Enter the NVM-Windows project release address: github.com/coreybutler…, select the latest release version nvm-setup.zip
Download:
After decompressing the compressed file, click to install:
Note here⚠️: NVM The installation and configuration path should not contain Chinese , because my Windows virtual machine is only allocated C drive, you can best install it to other user disks such as D drive:
After the installation is completed, enter NVM under CMD
or Powershell
, and you will find that the installation is complete:
This is the method I recommend. We download the NVM project file and perform manual configuration. Enter the project release address, download nvm-noinstall.zip
:
Extract it into a blank file, this folder is NVM address directory, for example, my address here is: D:\myEnvironment\nvm
After that, find the computer’s environment variables, such as Windows 10: Right-click This Computer
-Advanced System Settings
-Environment Variables
:
Finally, add environment variables:
NVM_HOME
: NVM address directory, for example: D:\myEnvironment\nvm
NVM_SYMLINK
: NVM Configure the soft link of Node.js. The directory needs to point to a directory that does not exist (it will be automatically created when NVM is used) , for example: D:\myEnvironment\nodejs
Append content to Path
, appended content:
%NVM_HOME% %NVM_SYMLINK%
安装完成后,在CMD
或者Powershell
下,输入NVM,即可发现安装完成:
中国大陆这边连接Node.js和NPM官方服务器有点困难,甚至不单单是下载慢了,有时候直接无法下载使用。所以我们换NVM和Node.js成国内源:
到你NVM安装路径,打开setting.txt文件(如果没有,则创建即可),更改:
root: D:\myEnvironment\nvm path: D:\myEnvironment\nodejs arch: 64 proxy: none node_mirror: https://npm.taobao.org/mirrors/node/ npm_mirror: https://npm.taobao.org/mirrors/npm/
这里解释一下参数:
%NVM_HOME%
%NVM_SYMLINK%
x64
,则填32
虽然可以使用项目包管理工具安装NVM(比如:Homebrew、APT),但还是推荐macOS和Linux使用手动配置方法(Git安装、常规安装),安装NVM,本文也是讲解使用非项目包管理器安装NVM。
官方脚本,需要连接Github,如果你的设备无法有效连接Github,请选择其他方法(如:常规安装、Git安装)
Terminal使用curl
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
或者使用wget
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
因为官方项目还在更新,这里粘贴脚本可能会过时。建议大家进入官方项目地址里进行粘贴。
安装好后,在根据你使用的Shell,在环境变量内追加:
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
一般macOS在~/.zshrc
内追加,Linux在没手动配置ZSH情况下,在~/.bashrc
内追加:
最后,在Terminal重载环境变量配置即可:
# For macOS/Linux With ZSH source ~/.zshrc # For Linux With Bash source ~/.bashrc
终端输入nvm命令,就不会报command not find
了,比如:
nvm -v
官方也推荐使用Git进行配置,但是官方的还是使用Github。国内的连接…… 所以,我推荐使用Gitee,在Terminal上一次输入:
# 进入家目录 cd ~ # 下载源码 git clone https://gitee.com/mirrors/nvm.git # 重命名为.nvm mv nvm .nvm
我们安装好NVM以后,我们需要配置到环境变量:
# Bash Shell用户 vim ~/.profile # ZSH Shell用户 vim ~/.zshrc
在环境变量内,追加:
export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
最后,在Terminal重载环境变量配置即可:
# For macOS/Linux With ZSH source ~/.zshrc # For Linux With Bash source ~/.bashrc
终端输入nvm命令,就不会报command not find
了,比如:
nvm -v
常规安装,其实就是手动实现Opt1
或Opt2
。手动下载nvm源码,并解压重命名为.nvm
。最后,按上文方法,添加
export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
到环境变量,重载即可。
大陆这边连接Node和NPM源有点忙,进而NVM也比较慢,所以我们使用前换成国内源。 临时使用:在终端内输入
export NVM_NODEJS_ORG_MIRROR=https://npm.taobao.org/mirrors/node/
需要长期使用,就配置到配置文件里。
Windows版本和macOS/Linux版本的NVM,操作基本一样,尤其是管理Node.js的命令;
本章节,的操作下,采用一个步骤两个图的模式(一张为Windwos版本NVM的操作截图,一张为macOS/Linux版本的操作截图)
nvm list
查看已经安装的版本:
如何查看通过NVM安装的Node.js版本呢? 你可以直接使用NVM命令:
# macOS/Linux nvm ls-remote --lts # Windows nvm list available
我们安装v12.21版本node:
我们安装好Node.js以后,需要激活
nvm use 12.21
测试一下可以使用用的:
如果你需要卸载NVM以及NVM所安装的Node.js,也很简单,且没有残留文件。
Windwos用户,如果是用安装器安装,使用其自带的反安装快捷方式即可。我们看看手动配置的方法如何卸载。
删除的地址,就是安装过程中的:
之后:右键
此电脑-高级系统设置
-环境变量
:
删除上文的NVM_HOME
、NVM_SYMLINK
以及PATH
内的:
%NVM_HOME% %NVM_SYMLINK%
macOS和Linux更简单了,终端执行:
# 删除NVM以及Node.js rm -rf ~/.nvm # 删除可能存在的NPM rm -rf ~/.npn
在环境变量内移除:
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
到此,NVM卸载完全。
在搭建Hexo博客的时候,目前(2021.07)最好还是使用Node.js v12。所以,我搭建Hexo博客,一般也喜欢切换Node.js到v12:
这里我先挖个坑,以后有机会和大家说说如何使用Node.js编译Minecraft的控制面板。
这个不用多说,安装VUE无法就那么几个方法。用Node.js的包管理工具NPM安装VUE再正常不过,运行也方便:
更多编程相关知识,请访问:编程视频!!
The above is the detailed content of A brief discussion on how NVM installs and manages multiple Node versions. For more information, please follow other related articles on the PHP Chinese website!