一、为什么要使用NVM
- 一句话来说:在一台电脑上管理多个node版本。
- 如果你不是个假前端,肯定遇到过某些组件限定了node版本。而当你开发维护多个项目的时候,尴尬的事情就发生了,你每次都需要重新安装合适的node版本。这个时候你就需要一个机灵的nvm了。
二、安装步骤
直接进入安装包下载地址:https://github.com/coreybutler/nvm-windows/releases ,选择nvm-setup.zip,下载后直接双击安装。
需要说的一点是安装过程中的两个地址。说明如下:
# nvm的安装地址,所有node文件的家
root: D:\nvm
# node对应环境变量的地址,当前正在使用的node的所在地址
path: D:\Program Files (x86)\nodejs
三、nvm镜像
在你的nvm安装地址下找到settings.txt的文本文件。双击打开后,在最后增加以下文本:
node_mirror: http://npm.taobao.org/mirrors/node/
npm_mirror: https://npm.taobao.org/mirrors/npm/
四、验证是否安装成功
打开cmd,运行nvm -v。所有能使用的命令都在下方展示啦。
C:\Users\zhang>nvm -v
Running version 1.1.7.
Usage:
nvm arch : Show if node is running in 32 or 64 bit mode.
nvm install <version> [arch] : The version can be a node.js version or "latest" for the latest stable version.
Optionally specify whether to install the 32 or 64 bit version (defaults to system arch).
Set [arch] to "all" to install 32 AND 64 bit versions.
Add --insecure to the end of this command to bypass SSL validation of the remote download server.
nvm list [available] : List the node.js installations. Type "available" at the end to see what can be installed. Aliased as ls.
nvm on : Enable node.js version management.
nvm off : Disable node.js version management.
nvm proxy [url] : Set a proxy to use for downloads. Leave [url] blank to see the current proxy.
Set [url] to "none" to remove the proxy.
nvm node_mirror [url] : Set the node mirror. Defaults to https://nodejs.org/dist/. Leave [url] blank to use default url.
nvm npm_mirror [url] : Set the npm mirror. Defaults to https://github.com/npm/cli/archive/. Leave [url] blank to default url.
nvm uninstall <version> : The version must be a specific version.
nvm use [version] [arch] : Switch to use the specified version. Optionally specify 32/64bit architecture.
nvm use <arch> will continue using the selected version, but switch to 32/64 bit mode.
nvm root [path] : Set the directory where nvm should store different versions of node.js.
If <path> is not set, the current root will be displayed.
nvm version : Displays the current running version of nvm for Windows. Aliased as v.
五、常用命令
- 查看当前电脑已安装和正在使用的node版本(带星号的就是正在使用的)
C:\Users\zhang>nvm list
* 11.11.0 (Currently using 64-bit executable)
10.7.0
8.15.0
8.12.0
2.安装对应版本node;
nvm install <version> [arch]
说明如上方命令介绍
The version can be a node.js version or "latest" for the latest stable version.
<version> 可以是node.js的某个具体版本,也可以是“latest”来安装最新稳定版本
Optionally specify whether to install the 32 or 64 bit version (defaults to system arch).
[arch] 选填 代表是安装32位还是64位的,默认安装与系统一致的arch
Set [arch] to "all" to install 32 AND 64 bit versions.
[arch]填入“all”时,两个版本都会安装。
C:\Users\zhang>nvm install latest
Version 11.11.0 is already installed.
C:\Users\zhang>nvm install v10.0.0/
Downloading node.js version 10.0.0 (64-bit)...
Complete
Creating D:\nvm\temp
Downloading npm version 5.6.0... Complete
Installing npm v5.6.0...
Installation complete. If you want to use this version, type
nvm use 10.0.0
3.切换node版本
C:\Users\zhang>nvm use 10.0.0
Now using node v10.0.0 (64-bit)
C:\Users\zhang>nvm ls
11.11.0
10.7.0
* 10.0.0 (Currently using 64-bit executable)
8.15.0
8.12.0
六、常见问题
- nvm use 切换node后,node命令无法使用
- 排查path地址是否为环境变量中配置的node_home的地址
- 排查path地址内是否有文件,若存在文件,先清空。(当存在文件时,nvm use命令无法将相应版本的node文件拷入,导致切换版本失败。)
网友评论