NVM是npm / Microsoft / Google推荐用于Windows的 Node.js版本管理器
NVM GitHub地址
NVM Windows GitHub地址
NVM Windows下载地址
下载地址中,nvm-noinstall.zip
是免安装版,nvm-setup.zip
是安装版。安装版可以自动设置环境变量。
在安装NVM for Windows之前,您需要卸载任何现有版本的node.js. 还需要删除任何现有nodejs安装目录。
以下是官方原文:
Please note, you need to uninstall any existing versions of node.js before installing NVM for Windows.
Also delete any existing nodejs installation directories.
NVM1.1.7安装版下载地址
D:\workprogram
next >
Setup - NVM for Windows 1.1.7
License Agreement
Please read the following important information before continuing.
Please read the followina License Aareement, You must accept the terms of this
agreement before continuina with the installation.
The MIT License (MIT)
Copyright (c) 2014 Corey Butler and contributors.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
(●) I accept the agreement
(○) I do not accept the agreement
Next > Cancel
出现提示框如下,问需要将nvm安装在哪个目录。点击Browse
,然后选择安装在:D:\workprogram\nvm
。注意:nvm的安装路径中最好只能是纯英文!然后点击next >
Setup NVM for Windows 1.1.7
Select Destination Location
Where should NVM for Windows be installed?
Setup will instal NM for Windows into the following folder.
To continue, dick Next. If you would like to select a different folder, dick Browse.
D: \ProgramFles\nvm Browse....
At least 6.6 MB of free disk space is required.
< back Next > Cancel
出现提示框如下,问你需要在哪个目录创建nodejs的快捷方式。点击Browse
,然后选择安装在D:\workprogram\nodejs
,然后点击next >
Setup NVM for Windows 1.1.7
Set Node.js Symlink
The active version of Node.is will always be available here.
Select the folder in which Setup should create the symlink, then dlick Next.
This directory will automatically be added to your system path.
D:\workprogram\nodejs Browse...
< Back Next > Cancel
最后点击install
,然后等待完成安装。设置注册表期间会被杀毒软件拦截,都同意即可。
设置一下npm和node的镜像代理,不需要可跳过这一步。修改settings.txt
文件,在最后新增行:
node_mirror: https://npm.taobao.org/mirrors/node/
npm_mirror: https://npm.taobao.org/mirrors/npm/
如有问题,查看遇到的问题
安装版教程到此结束,下面开始免安装版教程。
NVM1.1.7免安装版下载地址
D:\workprogram\nvm
目录D:\workprogram\nvm\
目录下创建settings.txt
文件,文件内容:root: D:\workprogram\nvm
path: D:\workprogram\nodejs
arch: 64
proxy: none
originalpath: .
originalversion:
node_mirror: https://npm.taobao.org/mirrors/node/
npm_mirror: https://npm.taobao.org/mirrors/npm/
创建D:\workprogram\nodejs
目录,用于node配置。
配置环境变量,添加变量名NVM_HOME
变量值D:\workprogram\nvm
。
变量名NVM_SYMLINK
变量值D:\workprogram\nodejs
。
修改Path环境变量,在Path环境变量末尾追加%NVM_HOME%;%NVM_SYMLINK%
(是追加!千万别替换掉了~)。如果是win10,则需要在Path中新建%NVM_HOME%
和%NVM_SYMLINK%
条目。
注意这一步需要在目录创建完后,及环境变量配置完后才进行。
使用管理员权限的cmd安装,以下是多条命令:
nvm install 15.12.0
npm install -g cnpm --registry=https://registry.npm.taobao.org
npm install -g yarn --registry=https://registry.npm.taobao.org
cnpm install -g @vue/cli
官方文档中介绍了一种GIT安装方式,安装起来挺简单的,而且linux平台通用;因此这里只说这种安装方式。
# 使用git下载项目
git clone https://github.com/nvm-sh/nvm.git ~/nvm
# 或者
git clone [email protected]:nvm-sh/nvm.git ~/nvm
这里是下载到 ~/nvm
目录(~表示用户目录)。
按照官方的说法,还需要编辑下面一个或多个文件。这里我是在~/.bash_profile
里加了代码,在~/.zshrc
末尾添加了一行source ~/.bash_profile
:
vim ~/.bash_profile
vim ~/.zshrc
vim ~/.profile
vim ~/.bashrc
添加内容:
export NVM_DIR="$HOME/nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
运行命令使其生效:
source ~/.zshrc
source ~/.bash_profile
下载并切换node版本:
nvm install 16.15.0
与windows中不同的是,需要彻底切换版本需要两个命令,否则打开新的控制台后出现zsh: command not found: npm
错误:
nvm use 16.15.0
nvm alias default 16.15.0
现在关闭原来的控制台新打开就可以使用npm啦。
来自官方文档,运行如下命令:
(
cd "$NVM_DIR"
git fetch --tags origin
git checkout `git describe --abbrev=0 --tags --match "v[0-9]*" $(git rev-list --tags --max-count=1)`
) && \. "$NVM_DIR/nvm.sh"
has a
globalconfig
and/or aprefix
setting, which are incompatible with nvm.
这是因为在${HOME}/.npmrc
中有了配置(cat ${HOME}/.npmrc
可查看),按照描述中的命令运行即可,比如我用的是v16.15.0
版本:
nvm use --delete-prefix v16.15.0
当环境变量没配正确时,重启也不会生效。在确保环境变量配置正确后,重启cmd或cmder控制台后会生效。如若不行则重启电脑后即会生效。原因未知
这个错误是由于安装目录中包含中文、字符,安装路径不是纯英文路径所导致的。
若是nvm use 版本号
时产生的错误,可打开有管理员权限的cmder或cmd后再使用命令。
执行nvm install 6.12.1
时发现npm
一直安装失败:
Downloading npm version 6.12.1... Download failed. Rolling Back.
查看淘宝镜像才发现,根本没有6.12.1
这个版本,镜像没更新这个版本的npm,所以使用nvm才安装不了。
解决方案-手动nvm:
node
官网下载对应版本的nodenvm
目录下创建名为v6.12.1
的文件夹node-v**.**.*.msi
的安装文件,将安装目录选为nvm\v6.12.1\
,然后一直next
即可。npm
的缓存目录:npm config set prefix "你的nvm目录\v6.12.1\node_modules"
现在可以通过nvm
切换到v6.12.1
版本的node
了:
nvm use 6.12.1
npm -v
报错没有npm命令,来自@qq_43950445的补充:
nvm install 了一个版本之后,也是要执行nvm+use+版本号 的命令后,才能使用npm这些命令
注:有些情况下,控制台会默认进行nvm use
操作。
yarn 的安装路径和缓存路径
查看各种路径命令
查看 yarn 全局bin位置
yarn global bin
查看 yarn 全局安装位置
yarn global dir
查看 yarn 全局cache位置
yarn cache dir
修改路径命令
改变 yarn 全局bin位置
yarn config set prefix "D:/nodejs/yarn/Data"
改变 yarn 全局安装位置
yarn config set global-folder "D:/nodejs/yarn/Data/global"
改变 yarn 全局cache位置
yarn config set cache-folder "D:/nodejs/yarn/Cache"
改变 yarn 全局 link 位置
yarn config set link-folder "D:/nodejs/yarn/Data/link"
设置源:
yarn config set registry http://registry.npm.taobao.org/
或者
npm config set registry http://registry.npm.taobao.org/
查看nvm命令列表 nvm --config
:
Running version 1.1.7.
Usage:
nvm arch : Show if node is running in 32 or 64 bit mode.
nvm install [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 : The version must be a specific version.
nvm use [version] [arch] : Switch to use the specified version. Optionally specify 32/64bit architecture.
nvm use 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 is not set, the current root will be displayed.
nvm version : Displays the current running version of nvm for Windows. Aliased as v
查看当前有的nvm版本命令 nvm list
。下面结果中前面“*”的表示当前版本:
16.15.0
* 12.22.7 (Currently using 64-bit executable)
12.13.1
此外还有:
nvm install 版本号
的安装命令nvm install latest
安装最新版本nvm install lts
安装最新长期支持版本nvm on
开启nvmnvm off
关闭nvmnvm use 版本号
切换nvm版本命令等其它命令
——
Update 2023-06-17 新增设置源的命令