


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:
- 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.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:
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
:
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/
这里解释一下参数:
- root:NVM的安装地址。即上文的
%NVM_HOME%
- path:激活node.js时的存储路径,即上文的
%NVM_SYMLINK%
- arch:系统架构,如果你的Windwos不是
x64
,则填32
- proxy:是否走代理
- node_mirror:node.js的下载源
- npm_mirror:npm的下载源
macOS/Linux配置方法
虽然可以使用项目包管理工具安装NVM(比如:Homebrew、APT),但还是推荐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
因为官方项目还在更新,这里粘贴脚本可能会过时。建议大家进入官方项目地址里进行粘贴。
安装好后,在根据你使用的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
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
Opt3:常规安装
常规安装,其实就是手动实现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/
需要长期使用,就配置到配置文件里。
管理与安装Node.js
Windows版本和macOS/Linux版本的NVM,操作基本一样,尤其是管理Node.js的命令;
本章节,的操作下,采用一个步骤两个图的模式(一张为Windwos版本NVM的操作截图,一张为macOS/Linux版本的操作截图)
1. 查看已经版本
nvm list
查看已经安装的版本:
2. 查看可安装版本
如何查看通过NVM安装的Node.js版本呢? 你可以直接使用NVM命令:
# macOS/Linux nvm ls-remote --lts # Windows nvm list available
3. 安装Node.js
我们安装v12.21版本node:
4. 激活Node.js版本
我们安装好Node.js以后,需要激活
nvm use 12.21
测试一下可以使用用的:
如何卸载
如果你需要卸载NVM以及NVM所安装的Node.js,也很简单,且没有残留文件。
Windwos
Windwos用户,如果是用安装器安装,使用其自带的反安装快捷方式即可。我们看看手动配置的方法如何卸载。
1. 删除NVM和Node.js软链接
删除的地址,就是安装过程中的:
- NVM_HOME:NVM地址目录,比如:D:\myEnvironment\nvm
- NVM_SYMLINK:NVM配置Node.js的软链。比如:D:\myEnvironment\nodejs
2. 删除环境变量
之后:右键
此电脑-高级系统设置
-环境变量
:
删除上文的NVM_HOME
、NVM_SYMLINK
以及PATH
内的:
%NVM_HOME% %NVM_SYMLINK%
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:
Minecraft面板
这里我先挖个坑,以后有机会和大家说说如何使用Node.js编译Minecraft的控制面板。
VUE
这个不用多说,安装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!

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools