nvm安装踩坑过程

NVM

简介

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安装版下载地址

  1. 下载nvm-setup.zip解压
  2. 点击安装,比如安装到:D:\workprogram
  3. 出现提示框如下,要你接受这个协议。选择I accept the agreement,然后点击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免安装版下载地址

  1. 解压到D:\workprogram\nvm目录
  2. 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%条目。

安装node、npm、yarn和vue-cli

注意这一步需要在目录创建完后,及环境变量配置完后才进行。

使用管理员权限的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 clone方式在mac或linux中安装

官方文档中介绍了一种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啦。

git方式升级

来自官方文档,运行如下命令:

(
  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"

git安装方式遇到的问题

has a globalconfig and/or a prefix setting, which are incompatible with nvm.

这是因为在${HOME}/.npmrc中有了配置(cat ${HOME}/.npmrc可查看),按照描述中的命令运行即可,比如我用的是v16.15.0版本:

nvm use --delete-prefix v16.15.0

遇到的问题

nvm在Cmder中不生效

当环境变量没配正确时,重启也不会生效。在确保环境变量配置正确后,重启cmd或cmder控制台后会生效。如若不行则重启电脑后即会生效。原因未知

exit status 1错误

这个错误是由于安装目录中包含中文、字符,安装路径不是纯英文路径所导致的。
若是nvm use 版本号时产生的错误,可打开有管理员权限的cmder或cmd后再使用命令。

npm Download failed

执行nvm install 6.12.1时发现npm 一直安装失败:

Downloading npm version 6.12.1... Download failed. Rolling Back.

查看淘宝镜像才发现,根本没有6.12.1这个版本,镜像没更新这个版本的npm,所以使用nvm才安装不了。

解决方案-手动nvm:

  1. 首先去node官网下载对应版本的node
  2. nvm目录下创建名为v6.12.1的文件夹
  3. 点开node-v**.**.*.msi的安装文件,将安装目录选为nvm\v6.12.1\,然后一直next即可。
  4. 修改这个版本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设置

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 开启nvm
  • nvm off 关闭nvm
  • nvm use 版本号 切换nvm版本命令

等其它命令

——
Update 2023-06-17 新增设置源的命令

你可能感兴趣的:(问题解决,windows,服务器,运维)