mac nvm 快速安装

nvm

安装

国内加速脚本

export NVM_SOURCE=https://gitee.com/mirrors/nvm.git
curl -o- https://gitee.com/mirrors/nvm/raw/master/install.sh | bash

检验安装是否成功

command -v nvm

执行后如果显示nvm则表示已经安装成功。

如果遇到nvm命令找不到的问题,如下。

FAQ

nvm 命令找不到(command not found)

需要手动配置环境变量。

执行命令echo $SHELL,根据结果判断写入的文件:

  • /bin/zsh => zsh => .zshrc
  • /bin/bash => bash => .bash_profile
.zshrc
echo 'export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' >> ~/.zshrc
source ~/.zshrc
.bash_profile
echo 'export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' >> ~/.bash_profile
source ~/.bash_profile

配置

1. 设置为系统默认 node

因为nvm安装的结果都是下面这样的格式:

/Users/neo/.nvm/versions/node/v14.17.4/bin/node

为了确保在所有的shell以及ide中都可以正常工作,我们需要设置把nvm安装的node设置为系统默认。

nvm alias default node
2. 设置 node 镜像

这里是给安装node设置镜像。

export NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node
nvm install node


// 或者
NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node nvm install 4.2

长期替换可以使用下面的设置:

echo 'export NVM_NODEJS_ORG_MIRROR="https://npmmirror.com/mirrors/node"' >> ~/.zshrc

注意! 上面脚本是把配置写入文件.zshrc,你如果对此不了解,请参考FAQ,确定是否更换为.bash_profile

更新

重新执行安装脚本即可。

安装 node

安装最新版本:

nvm install node # "node"代表最新版本

安装指定版本:

nvm install 14.16.1 # or 10.10.0, 8.9.1, etc

通过ls-remote可以查看所有可安装的版本列表:

nvm ls-remote

然后使用use启用安装的版本:

// 最新版本
nvm use node


// 指定版本
nvm use 6.14.4

你也可以通过which命令查看已安装node所在目录:

nvm which 5.0

通过以下命令可以安装最新版npm

nvm install-latest-npm

设置终端默认 node

nvm包含以下几个别名(alias):

  • node: 已安装的node最新版
  • iojs: 已安装的io.js最新版
  • stable: 已废弃,现在是node的别名
  • unstable: 已废弃

当我们安装了新版的node之后,nvm默认还会指向旧版本,通过执行以下命令将node别名指向到最新版。

这样可以解决node找不到的问题。

nvm alias default node

如果你不想指向已安装的最新版node,可以直接指向版本号,比如:

nvm alias default 16.20.1

多 node 环境下使用

在项目中使用不同版本 node

你可以在项目根目录中创建.nvmrc来指定node版本,文件写入想要的版本号。

14.16.1

然后在根目录使用nvm use即可,缺点就是每次都要手动执行,如果想实现自动化可以按照下面的设置。

bash

把下面内容追加到~/.bashrc

cdnvm() {
    cd "$@";
    nvm_path=$(nvm_find_up .nvmrc | tr -d '\n')


    # If there are no .nvmrc file, use the default nvm version
    if [[ ! $nvm_path = *[^[:space:]]* ]]; then


        declare default_version;
        default_version=$(nvm version default);


        # If there is no default version, set it to `node`
        # This will use the latest version on your machine
        if [[ $default_version == "N/A" ]]; then
            nvm alias default node;
            default_version=$(nvm version default);
        fi


        # If the current version is not the default version, set it to use the default version
        if [[ $(nvm current) != "$default_version" ]]; then
            nvm use default;
        fi


        elif [[ -s $nvm_path/.nvmrc && -r $nvm_path/.nvmrc ]]; then
        declare nvm_version
        nvm_version=$(<"$nvm_path"/.nvmrc)


        declare locally_resolved_nvm_version
        # `nvm ls` will check all locally-available versions
        # If there are multiple matching versions, take the latest one
        # Remove the `->` and `*` characters and spaces
        # `locally_resolved_nvm_version` will be `N/A` if no local versions are found
        locally_resolved_nvm_version=$(nvm ls --no-colors "$nvm_version" | tail -1 | tr -d '\->*' | tr -d '[:space:]')


        # If it is not already installed, install it
        # `nvm install` will implicitly use the newly-installed version
        if [[ "$locally_resolved_nvm_version" == "N/A" ]]; then
            nvm install "$nvm_version";
        elif [[ $(nvm current) != "$locally_resolved_nvm_version" ]]; then
            nvm use "$nvm_version";
        fi
    fi
}
alias cd='cdnvm'
cd $PWD
zsh

把下面内容追加到~/.zshrc

# place this after nvm initialization!
autoload -U add-zsh-hook
load-nvmrc() {
  local node_version="$(nvm version)"
  local nvmrc_path="$(nvm_find_nvmrc)"


  if [ -n "$nvmrc_path" ]; then
    local nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")


    if [ "$nvmrc_node_version" = "N/A" ]; then
      nvm install
    elif [ "$nvmrc_node_version" != "$node_version" ]; then
      nvm use
    fi
  elif [ "$node_version" != "$(nvm version default)" ]; then
    echo "Reverting to nvm default version"
    nvm use default
  fi
}
add-zsh-hook chpwd load-nvmrc
load-nvmrc

安装时迁移全局 npm 包

执行时通过reinstall-packages-from参数指定旧的node版本,这样可以把指定版本内npm全局安装的包迁移到当前的node版本。

nvm install v12.0.0 --reinstall-packages-from=10.0

原文章:https://brew.idayer.com/install/nvm-for-nodejs/

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值