node 版本管理器 之 nvm
一、前言
开发的时候会遇到 node 版本过高或者过低,每个项目要求的版本是不一样的,这时候我们就需要使用到各个版本的 node,需要转换到对应的版本。使用 nvm 可以切换 node 版本
,很好的解决了这个问题
二、安装 nvm
2.1 window 10 安装
官方下载地址,上面有很多版本,下载最新版本,也就是压缩包就可以了,nvm-setup.zip
。
- 下载压缩包
nvm-setup.zip
- 解压压缩包
- 右键以
管理员
运行安装,如果没有以管理员运行,安装之后无法使用 - 安装路径选择,任意位置,一般就是默认就好,但是要注意,
路径不要是中文,不要有空格
,否则安装之后无法使用 - 若提示修改系统变量,点击全部允许
三、安装失败 以及其他各种问题
3.1 nvm windows 安装失败
λ nvm -v
bash: nvm: command not found # 无法找到 nvm 命令
如果已经以管理员运行,并且保证路径没有中文没有空格。
- 1.重启电脑试试
- 2.可能是安装的时候变量配置不成功(自动配置环境变量如果不行那就手动吧),手动配置见 第 4 章
3.2 nvm 下载慢的问题
在程序安装目录下找到 settings.txt,添加下面两行。
root: C:\Program Files\nvm
path: C:\Program Files\nodejs
node_mirror: https://npm.taobao.org/mirrors/node/ # 添加
npm_mirror: https://npm.taobao.org/mirrors/npm/ # 添加
3.3 nvm 无法切换源(nodeJS)
# 正常切换
nvm use 8.0.0
Now using node v8.0.0 (64-bit) # 这个提示正常来讲是切换成功的
node -v
v7.6.4 # 还是原来版本
卸载原来已安装的 node, 与全局安装的包
四、手动配置环境变量
4.1 打开环境变量配置
方法一:右键桌面我的电脑->属性->高级系统配置->高级->坏境变量
方法二:电脑开始处搜索 sysdm.cpl
打开 ->高级->坏境变量
4.2 新建变量-配置
如果系统变量确实没有得到配置的话,需要在这里手动创建。下面的的图片这里变量其实已经存在了,如果不存在,那么就点击新建,
新建系统变量,自定义名称 例如:NVM_HOME,地址是 nvm 安装路径
创建变量之后还有配置路径 path,点击 path 变量,在这里新建,%NVM_HOME%
,这里的百分号内的名称与新建的名称需要一致。
五、如何使用 nvm
# 查看 nvm 版本
$ nvm -v
# 安装与卸载各个 nodeJS 版本
$ nvm install latest # 下载最新的 node 版本
$ nvm install 4.4.4 # 安装 4.4.4 版本 的 node
$ nvm install 6.2.0 32 # 安装 6.2.0 版本 32位系统的 node(默认是 64 位,32 位需指定)
$ nvm uninstall 6.2.0 # 卸载对应的版本
# 切换 nodeJS 版本
$ nvm use 7.2.0 # 当前使用 7.2.0 版本的 nodeJS
# 查看已安装的 node 版本
$ nvm list
# * 10.15.0 (Currently using 64-bit executable) # 指的系统环境当前使用的nodeJS 版本
# 7.2.0
# 6.2.0
# 4.4.4
# 更多快捷方法/命令 查看
$ nvm # 现在 nvm -v 查看版本的命令也可以查看其他的命令
λ 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.