Mac系统国内通过nvm快速安装node

2024-05-28 20:30:16 浏览数 (1)

国内通过nvm安装node

国内nvm安装工具

地址:https://gitee.com/RubyMetric/nvm-cn

安装命令

代码语言:shell复制
bash -c "$(curl -fsSL https://gitee.com/RubyMetric/nvm-cn/raw/main/install.sh)"

如果按照过程中有报错可以尝试下载安装脚本

在浏览器中打开下面的链接https://gitee.com/RubyMetric/nvm-cn/raw/main/install.sh,复制内容。

新建install.sh脚本vim install.sh粘贴复制的内容(或者直接复制下面的内容粘贴到脚本里面)

代码语言:shell复制
#!/usr/bin/env bash
# ---------------------------------------------------------------
# File          : install.sh
# Authors       : ccmywish <ccmywish@qq.com>
#                 aifuxi   <aifuxi.js@gmail.com>
# Created on    : <2021-01-06>
# Last modified : <2023-07-20>
#
# install:
#
#   This file installs nvm and change Node.JS source on your linux
#
# ----------
# Changelog:
#
# Note that, the '@user' represents the user id of Gitee
#
# ~> v0.1.0
# <2023-07-20> Gitee追踪的仓库被删除,暂时启用我们的备用仓库 by @ccmywish
# <2023-05-09> Check Git first and add CI on Gitee by @ccmywish
# <2023-05-01> Update nvm repo source by @ccmywish
# <2022-11-14> Update Node.JS mirror source by @aifuxi
# <2021-01-06> Create file by @ccmywish
# ---------------------------------------------------------------

# Exit on error
set -e

if ! [ $(command -v "git") ]; then
  echo "=> 请您首先安装Git"
  exit 127
fi

export NVM_DIR="$HOME/.nvm" && (
  echo "=> Git clone nvm"
  git clone https://gitee.com/RubyMetric/nvm-official "$NVM_DIR" # 备用仓库
  # git clone https://gitee.com/mirrors_nvm-sh/nvm "$NVM_DIR"  # Gitee提供的追踪上游仓库
  cd "$NVM_DIR"
  git checkout `git describe --abbrev=0 --tags --match "v[0-9]*" $(git rev-list --tags --max-count=1)`
) && . "$NVM_DIR/nvm.sh"


echo "=> 添加nvm环境变量(Bash,Zsh)"
echo -e "n# nvm config" >> ~/.bashrc
echo -e "n# nvm config" >> ~/.zshrc

echo "export NVM_DIR="$HOME/.nvm"" >> ~/.bashrc
echo "export NVM_DIR="$HOME/.nvm"" >> ~/.zshrc
echo "[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm"  >> ~/.bashrc
echo "[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm"  >> ~/.zshrc


echo "=> 使用淘宝镜像"
echo "export NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node" >> ~/.bashrc
echo "export NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node" >> ~/.zshrc

echo "=> 安装nvm-update,升级更新请使用该命令"
curl -fsSL https://gitee.com/RubyMetric/nvm-cn/raw/main/nvm-update.sh -o nvm-update.sh
chmod  x ./nvm-update.sh

if ! [ -v NVM_CN_IN_CI ];then
  sudo mv ./nvm-update.sh /usr/local/bin/nvm-update
fi

echo "=> 安装完成!"
echo "=> 最后请您重启终端"
echo

执行安装脚本

代码语言:shell复制
sh install.sh

使用

代码语言:shell复制
nvm ls

# 列出所有可安装版本
nvm ls-remote

# 安装某个版本Node
nvm install lts/fermium
nvm install v12.20.1
nvm install v15.5.1

# 切换Node版本
nvm use system
nvm use 14.15    # 不用全部打出版本号

# 更新nvm
nvm-update

npm 换源

代码语言:shell复制
npm config set registry https://registry.npmjs.org/

如果要使用chsrc将挑选最快镜像站需要可以安装RubyMetric/chsrc

卸载命令

代码语言:shell复制
bash -c "$(curl -fsSL https://gitee.com/RubyMetric/nvm-cn/raw/main/uninstall.sh)"

0 人点赞