使用git安装nvm

1,360 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

安装nvm方法官方git地址:github.com/nvm-sh/nvm

Git Install

image.png

If you have git installed (requires git v1.7.10+):

clone this repo in the root of your user profile

cd ~/ from anywhere then git clone https://github.com/nvm-sh/nvm.git .nvm

cd ~/.nvm and check out the latest version with git checkout v0.39.0

activate nvm by sourcing it from your shell: . ./nvm.sh

Now add these lines to your ~/.bashrc , ~/.profile, or ~/.zshrc file to have it automatically sourced upon login: (you may have to add to more than one of the above files)

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

在这里插入图片描述

使用 cnpm 加速 npm,安装淘宝镜像

 npm --registry=https://registry.npm.taobao.org install cnpm -g

我这里已经安装过了 

4.查看cnpm是否真安装成功 

cnpm -v

nvm常用命令

nvm install <version> ## 安装指定版本,可模糊安装,如:安装v4.4.0,既可nvm install v4.4.0,又可nvm install 4.4
nvm uninstall <version> ## 删除已安装的指定版本,语法与install类似
nvm use <version> ## 切换使用指定的版本node
nvm ls ## 列出所有安装的版本
nvm ls-remote ## 列出所以远程服务器的版本(官方node version list)
nvm current ## 显示当前的版本
nvm alias <name> <version> ## 给不同的版本号添加别名
nvm unalias <name> ## 删除已定义的别名
nvm reinstall-packages <version> ## 在当前版本node环境下,重新全局安装指定版本号的npm包

Example:

$ nvm use 16
Now using node v16.9.1 (npm v7.21.1)
$ node -v
v16.9.1
$ nvm use 14
Now using node v14.18.0 (npm v6.14.15)
$ node -v
v14.18.0
$ nvm install 12
Now using node v12.22.6 (npm v6.14.5)
$ node -v
v12.22.6

附:MAC 打开.bash_profile 启动终端Terminal 进入当前用户的home目录 输入cd ~ 创建.bash_profile 输入touch .bash_profile 编辑.bash_profile文件 输入open -e .bash_profile 保存文件,关闭.bash_profile 更新刚配置的环境变量 输入source .bash_profile