在window上管理nodejs版本,安装nvm-windows
第一次安装使用nvm-windows,会遇到好多坑,请仔细按一下步骤安装:
-
在Windows下完全卸载已安装的node.js
- 从卸载程序卸载程序和功能,也可以直接右键node.js的安装包并选择卸载。
- 重新启动(或者重新启动任务管理器杀死所有与节点相关的进程)。
- 从下列的目录中找到相关的内容并删除掉:
- C:\Program Files (x86)\nodejs
- C:\Program Files\nodejs
- C:\Users{User}\AppData\Roaming\npm(或%appdata%\npm)
- C:\Users{User}\AppData\Roaming\npm-cache(或%appdata%\npm-cache)
- 检查%PATH%环境变量以确保没有引用Nodejs或npm存在。
- 重新启动电脑。
-
下载nvm-windows
地址:https://github.com/coreybutler/nvm-windows/releases
下载最新版本安装包即可:
解压后里面是nvm-setup.exe安装文件,默认安装即可,记住默认安装哦,不然安装node时有可能没有权限而无法创建node快捷方式。
打开CMD窗口,直行nvm,如果显示版本等信息,则说明安装成功。
C:\Users\Administrator>nvm
Running version 1.1.8.
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 ue 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不是内部或外部命令,则需要做些检查。
C:\Users\qdz>nvm
'nvm' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
第一步,检查是否配置了path环境变量
第二步:如果上面环境变量都存在,则就要重启电脑,再次执行nvm则成功。
在使用之前一定要更换下载源
如果不更换下载源,会出现安装失败。也可能出现node安装成功,npm安装失败问题。
下面演示的就是,因未更换下载源,node安装成功,但npm安装失败。
C:\Users\Administrator>nvm install 15.0.1
Downloading node.js version 15.0.1 (64-bit)...
Complete
Creating C:\Users\Administrator\AppData\Roaming\nvm\temp
Downloading npm version 7.0.3... Error while downloading https://github.com/npm/cli/archive/v7.0.3.zip - Get "https://github.com/npm/cli/archive/v7.0.3.zip": read tcp 192.168.10.224:58463->20.205.243.166:443: wsarecv: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x20 pc=0x2fa5a7]
goroutine 1 [running]:
nvm/web.Download(0x11810300, 0x2d, 0x11812400, 0x3e, 0x11bd7c10, 0x5, 0x0)
C:/Users/corey/OneDrive/Documents/workspace/oss/coreybutler/nvm-windows/src/web/web.go:81 +0x1c7
nvm/web.GetNpm(0x11810481, 0x2a, 0x11bd7c10, 0x5, 0x119a4994)
C:/Users/corey/OneDrive/Documents/workspace/oss/coreybutler/nvm-windows/src/web/web.go:209 +0x22e
main.install(0x1180a0c8, 0x6, 0x372ad3, 0x2)
C:/Users/corey/OneDrive/Documents/workspace/oss/coreybutler/nvm-windows/src/nvm.go:295 +0x82c
main.main()
C:/Users/corey/OneDrive/Documents/workspace/oss/coreybutler/nvm-windows/src/nvm.go:86 +0x88f
更换下载源
nvm默认安装目录为:C:\Users\Administrator[当前windows用户名]\AppData\Roaming\nvm
进入 nvm 安装目录,编辑 setting.txt,追加两行代码:
node_mirror: https://npmmirror.com/mirrors/node/
npm_mirror: https://npmmirror.com/mirrors/npm/
接下来我们开始使用nvm。
nvm list
C:\Users\Administrator>nvm list
No installations recognized.
nvm list available
输入该命令结果如下:
| CURRENT | LTS | OLD STABLE | OLD UNSTABLE |
|--------------|--------------|--------------|--------------|
| 17.0.1 | 14.18.1 | 0.12.18 | 0.11.16 |
| 17.0.0 | 14.18.0 | 0.12.17 | 0.11.15 |
| 16.12.0 | 14.17.6 | 0.12.16 | 0.11.14 |
| 16.11.1 | 14.17.5 | 0.12.15 | 0.11.13 |
| 16.11.0 | 14.17.4 | 0.12.14 | 0.11.12 |
| 16.10.0 | 14.17.3 | 0.12.13 | 0.11.11 |
| 16.9.1 | 14.17.2 | 0.12.12 | 0.11.10 |
| 16.9.0 | 14.17.1 | 0.12.11 | 0.11.9 |
| 16.8.0 | 14.17.0 | 0.12.10 | 0.11.8 |
| 16.7.0 | 14.16.1 | 0.12.9 | 0.11.7 |
| 16.6.2 | 14.16.0 | 0.12.8 | 0.11.6 |
| 16.6.1 | 14.15.5 | 0.12.7 | 0.11.5 |
| 16.6.0 | 14.15.4 | 0.12.6 | 0.11.4 |
| 16.5.0 | 14.15.3 | 0.12.5 | 0.11.3 |
| 16.4.2 | 14.15.2 | 0.12.4 | 0.11.2 |
| 16.4.1 | 14.15.1 | 0.12.3 | 0.11.1 |
| 16.4.0 | 14.15.0 | 0.12.2 | 0.11.0 |
| 16.3.0 | 12.22.7 | 0.12.1 | 0.9.12 |
| 16.2.0 | 12.22.6 | 0.12.0 | 0.9.11 |
| 16.1.0 | 12.22.5 | 0.10.48 | 0.9.10 |
This is a partial list. For a complete list, visit https://nodejs.org/download/releases
这边展示的版本并不全,可以按上面的指引去nodejs官网查看可用版本https://nodejs.org/en/download/releases/,但是官网更新之后这个地址看不到所有版本了,只能看到每个版本的最新稳定版本,要想看到所有版本,请查看这个https://nodejs.org/dist/。
nvm install latest
nvm install 17.0.0
nvm use 17.0.0
nvm uninstall 17.0.0
上面就是nvm-windows的整个安装过程了,如有疑问,请私信。
附录:nvm 常用指令
nvm 提供了强大而简洁的命令:
nvm arch [32|64]: 显示 node.js 运行在 32位 还是 64位架构. 设置 32/64 可以覆盖默认的系统架构.
nvm install [arch]: 安装特定版本. 版本可以填入具体的 node.js 版本号或“latest”表示最新的稳定版本.(可选)指定是安装32位版本还是64位版本(默认为系统架构). 将[arch]设置为all以安装32位和64位版本.
nvm list [available]: 列出所有已安装的 node.js 版本. 键入 available 在结尾可以显示当前可用于下载的所有版本列表.
nvm on: 启用 node.js 版本管理.
nvm off: 禁用 node.js 版本管理 (并不会卸载任何已安装的版本).
nvm proxy [url]: 设置用于下载的代理. 将[url]留空以查看当前代理。将[url]设置为“none”以删除代理.
nvm uninstall: 卸载特定版本.
nvm use [arch]: 切换到指定的版本. 可选地指定32/64位体系结构.nvm use将继续使用所选版本,但根据提供给``的值切换到32/64位模式。有关在特定目录中使用“use”(或使用“.nvmrc”)的信息,请参阅 issue #16.
nvm root: 指定一个存放 node.js 不同版本的根目录. 如果为未设置任何值,则会显示当前已设置的目录.
nvm version: 显示 nvm-windows 的当前版本.
nvm node_mirror: 设置 node 镜像源. 中国用户请使用 npm.taobao.org/mirrors/nod…
nvm npm_mirror: 设置 npm 镜像源. 中国用户请使用 npm.taobao.org/mirrors/npm…
总结问题:
- nvm use 17.7.1 报错
exit status 1: ???д???
类似上面错误,是因为权限不足,管理员身份运行cmd或PowerShell,再次执行,成功。