Home  >  Article  >  Web Front-end  >  A brief discussion on how NVM installs and manages multiple Node versions

A brief discussion on how NVM installs and manages multiple Node versions

青灯夜游
青灯夜游forward
2021-11-12 10:19:462576browse

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!

A brief discussion on how NVM installs and manages multiple Node versions

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:

  • Linux (x86 architecture & ARM architecture)
  • macOS (x86 Architecture & ARM architecture)
  • Windows (X86 architecture, ARM architecture not tested)

Warning⚠️: It is highly not recommended to use the n module under NPM to manage node versions

Auxiliary video

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

Environment preparation

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.

Windows configuration method

Configure NVM under Windows. According to the configuration method of NVM project address, there are two methods:

  • Installer Installation
  • Manual configuration (recommended)

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.

Opt1: Installer

Enter the NVM-Windows project release address: github.com/coreybutler…, select the latest release version nvm-setup.zipDownload:

A brief discussion on how NVM installs and manages multiple Node versions

After decompressing the compressed file, click to install:

A brief discussion on how NVM installs and manages multiple Node versions

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:

A brief discussion on how NVM installs and manages multiple Node versions

A brief discussion on how NVM installs and manages multiple Node versions

After the installation is completed, enter NVM under CMD or Powershell, and you will find that the installation is complete:

A brief discussion on how NVM installs and manages multiple Node versions

Opt2: Manual configuration [Push]

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:

A brief discussion on how NVM installs and manages multiple Node versions

Extract it into a blank file, this folder is NVM address directory, for example, my address here is: D:\myEnvironment\nvm

A brief discussion on how NVM installs and manages multiple Node versions

After that, find the computer’s environment variables, such as Windows 10: Right-click This Computer-Advanced System Settings-Environment Variables:

A brief discussion on how NVM installs and manages multiple Node versions

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

A brief discussion on how NVM installs and manages multiple Node versions

Append content to Path, appended content:

%NVM_HOME%
%NVM_SYMLINK%

A brief discussion on how NVM installs and manages multiple Node versions

安装完成后,在CMD或者Powershell下,输入NVM,即可发现安装完成:

1A brief discussion on how NVM installs and manages multiple Node versions

配置国内源

中国大陆这边连接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/

1A brief discussion on how NVM installs and manages multiple Node versions

这里解释一下参数:

  • root:NVM的安装地址。即上文的%NVM_HOME%
  • path:激活node.js时的存储路径,即上文的%NVM_SYMLINK%
  • arch:系统架构,如果你的Windwos不是x64,则填32
  • proxy:是否走代理
  • node_mirror:node.js的下载源
  • npm_mirror:npm的下载源

macOS/Linux配置方法

虽然可以使用项目包管理工具安装NVM(比如:HomebrewAPT),但还是推荐macOS和Linux使用手动配置方法(Git安装、常规安装),安装NVM,本文也是讲解使用非项目包管理器安装NVM。

Opt1:官方脚本

官方脚本,需要连接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

1A brief discussion on how NVM installs and manages multiple Node versions

因为官方项目还在更新,这里粘贴脚本可能会过时。建议大家进入官方项目地址里进行粘贴。

安装好后,在根据你使用的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内追加:

1A brief discussion on how NVM installs and manages multiple Node versions

最后,在Terminal重载环境变量配置即可:

# For macOS/Linux With ZSH
source ~/.zshrc
# For Linux With Bash
source ~/.bashrc

终端输入nvm命令,就不会报command not find了,比如:

nvm -v

1A brief discussion on how NVM installs and manages multiple Node versions

Opt2:Git安装

官方也推荐使用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

1A brief discussion on how NVM installs and manages multiple Node versions

Opt3:常规安装

常规安装,其实就是手动实现Opt1Opt2。手动下载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/

需要长期使用,就配置到配置文件里。

管理与安装Node.js

Windows版本和macOS/Linux版本的NVM,操作基本一样,尤其是管理Node.js的命令;

本章节,的操作下,采用一个步骤两个图的模式(一张为Windwos版本NVM的操作截图,一张为macOS/Linux版本的操作截图)

1. 查看已经版本

nvm list

查看已经安装的版本:

1A brief discussion on how NVM installs and manages multiple Node versions

1A brief discussion on how NVM installs and manages multiple Node versions

2. 查看可安装版本

如何查看通过NVM安装的Node.js版本呢? 你可以直接使用NVM命令:

# macOS/Linux
nvm ls-remote --lts
# Windows
nvm list available

1A brief discussion on how NVM installs and manages multiple Node versions

A brief discussion on how NVM installs and manages multiple Node versions

3. 安装Node.js

我们安装v12.21版本node:

2A brief discussion on how NVM installs and manages multiple Node versions

2A brief discussion on how NVM installs and manages multiple Node versions

4. 激活Node.js版本

我们安装好Node.js以后,需要激活

nvm use 12.21

测试一下可以使用用的:

2A brief discussion on how NVM installs and manages multiple Node versions

2A brief discussion on how NVM installs and manages multiple Node versions

如何卸载

如果你需要卸载NVM以及NVM所安装的Node.js,也很简单,且没有残留文件。

Windwos

Windwos用户,如果是用安装器安装,使用其自带的反安装快捷方式即可。我们看看手动配置的方法如何卸载。

1. 删除NVM和Node.js软链接

删除的地址,就是安装过程中的:

  • NVM_HOME:NVM地址目录,比如:D:\myEnvironment\nvm
  • NVM_SYMLINK:NVM配置Node.js的软链。比如:D:\myEnvironment\nodejs

2A brief discussion on how NVM installs and manages multiple Node versions

2. 删除环境变量

之后:右键此电脑-高级系统设置-环境变量:

2A brief discussion on how NVM installs and manages multiple Node versions

删除上文的NVM_HOMENVM_SYMLINK以及PATH内的:

%NVM_HOME%
%NVM_SYMLINK%

2A brief discussion on how NVM installs and manages multiple Node versions

macOS/Linux

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博客

在搭建Hexo博客的时候,目前(2021.07)最好还是使用Node.js v12。所以,我搭建Hexo博客,一般也喜欢切换Node.js到v12:

2A brief discussion on how NVM installs and manages multiple Node versions

Minecraft面板

这里我先挖个坑,以后有机会和大家说说如何使用Node.js编译Minecraft的控制面板。

VUE

这个不用多说,安装VUE无法就那么几个方法。用Node.js的包管理工具NPM安装VUE再正常不过,运行也方便:

2A brief discussion on how NVM installs and manages multiple Node versions

更多编程相关知识,请访问:编程视频!!

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!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete