因为要去豆瓣爬虫获取自己的电影,但是总是失败,获取不到,看到评论发现v12.18版本的可以实现,但是自己的开发需要版本是16版本的,所以在考虑是否可以多个版本来回切换呢,所以看了很多总算是可以了:nvm,可以让你在电脑上管理多个版本的nodejs

卸载已有node

为了降低后面的麻烦,免去不必要的问题和时间浪费,建议先删除自身已有的,后面在安装

下载nvm

这里可以选择以下两种方式下载:

1
E:\RuanJianFile\NodeJS_File\nvm

当安装完成后,可以用cmd输入nvm来进行测试,当出现以下信息说明安装OK

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
PS C:\Users\28138> nvm
Running version 1.1.10.
Usage:
nvm arch : Show if node is running in 32 or 64 bit mode.
nvm current : Display active version.
nvm install <version> [arch] : The version can be a specific version, "latest" for the latest current version, or "lts" for the
most recent LTS 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.

安装淘宝npm镜像

如果有安装淘宝镜像的需完成下载镜像的配置,没有可跳过

打开nvm文件夹下的settings.txt文件,在最后添加以下代码(不改,下载node可能会报错)

1
2
3
4
arch: 64
proxy:
node_mirror: https://npm.taobao.org/mirrors/node/
npm_mirror: https://npm.taobao.org/mirrors/npm/

🍓 简单解释
root:NVM 安装路径
path:NodeJS 快捷方式路径

使用

  • NVM 安装成功后,win + r 输入cmd 新开cmd窗口,可以通过 nvm -v 命令查看所有可用的命令。【简单看一下即可,后面会列出常用的命令和解释】
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
C:\Users\28138>nvm
Running version 1.1.10.
Usage:
nvm arch : Show if node is running in 32 or 64 bit mode.
nvm current : Display active version.
nvm install <version> [arch] : The version can be a specific version, "latest" for the latest current version, or "lts" for the
most recent LTS 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 use "latest", "lts", or "newest".
"newest" is the latest installed 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.
  • 使用 nvm install <版本号>命令安装指定版本的NodeJS
1
2
#这里以v12.18.1为例
nvm install v12.18.1

注意:如果只有Downloading node.js version 12.18.1(64-bit)...一直持续如下,应该是没有安装成功npm,这是可以按上面设置淘宝镜像 找到nvm目录 在settings.txt文件 里添加两行配置 走一下淘宝镜像,即可成功解决

安装成功后在 nvm 安装目录下出现一个 v12.18.1 文件夹,这时可以尝试在上面使用 nvm list 命令查看已安装 NodeJS 列表。

1
2
3
4
# 这里因为自己有安装过需要的版本,所以有两个,正常到这里是一个
C:\Users\28138>nvm list
16.17.1
* 12.18.1 (Currently using 64-bit executable)

剩下的大家可以根据自己的所需进行安装其他版本

🍓 颜色:yellow ;展开参数:open; 特别注意

运行中的问题

1.可能会出现乱码问题,例如如下情况

1
2
C:\>nvm use v12.18.2
exit status 1: ��û���㹻��Ȩ��ִ�д˲�����

解决方案:重新打开并以管理员身份运行

切换使用的node版本

这个就比较简单了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
C:\Users\28138>nvm use v12.18.1    #用nvm use v+您安装的Node版本号
Now using node v12.18.1 (64-bit) # 提示这句说明切换成功
C:\Users\28138>npm -v # 可以打印npm 的版本
6.14.5
C:\Users\28138>node -v # 打印node版本
v12.18.1
C:\Users\28138>nvm list # 通过命令也可以查看现在使用的 版本
16.17.1
* 12.18.1 (Currently using 64-bit executable)
# 以下是切换到另一个版本的,这里就不过多阐述了
C:\Users\28138>nvm use v16.17.1
Now using node v16.17.1 (64-bit)
C:\Users\28138>npm -v
8.15.0
C:\Users\28138>node -v
v16.17.1
C:\Users\28138>nvm list
* 16.17.1 (Currently using 64-bit executable)
12.18.1

常用的nvm命令

1
2
3
4
5
6
7
8
9
nvm off                     // 禁用node.js版本管理(不卸载任何东西)
nvm on // 启用node.js版本管理
nvm install <version> // 安装node.js的命名 version是版本号 例如:nvm install 8.12.0
nvm uninstall <version> // 卸载node.js是的命令,卸载指定版本的nodejs,当安装失败时卸载使用
nvm ls // 显示所有安装的node.js版本
nvm list available // 显示可以安装的所有node.js的版本
nvm use <version> // 切换到使用指定的nodejs版本
nvm v // 显示nvm版本
nvm install stable // 安装最新稳定版