Mac安装nvm
如默
撰写于 2023年 03月 02 日

说明

刚刚买了Mac mini,配置了一下环境,正好需要安装node,记录一下

步骤

本文使用brew安装,确保已经安装了brew包管理工具,若未安装,请参考:

这里先记录一下nvm官网,安装执行:

brew install nvm

安装成功会显示如下内容:

==> Downloading https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api/formul
######################################################################## 100.0%
==> Downloading https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api/cask.j
######################################################################## 100.0%
==> Fetching nvm
==> Downloading https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/nvm-0.39.3
######################################################################## 100.0%
==> Pouring nvm-0.39.3.all.bottle.tar.gz
==> Caveats
Please note that upstream has asked us to make explicit managing
nvm via Homebrew is unsupported by them and you should check any
problems against the standard nvm install method prior to reporting.

You should create NVM's working directory if it doesn't exist:
  mkdir ~/.nvm

Add the following to your shell profile e.g. ~/.profile or ~/.zshrc:
  export NVM_DIR="$HOME/.nvm"
  [ -s "/opt/homebrew/opt/nvm/nvm.sh" ] && \. "/opt/homebrew/opt/nvm/nvm.sh"  # This loads nvm
  [ -s "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm" ] && \. "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm"  # This loads nvm bash_completion

You can set $NVM_DIR to any location, but leaving it unchanged from
/opt/homebrew/Cellar/nvm/0.39.3 will destroy any nvm-installed Node installations
upon upgrade/reinstall.

Type `nvm help` for further information.
==> Summary
🍺  /opt/homebrew/Cellar/nvm/0.39.3: 9 files, 190.5KB
==> Running `brew cleanup nvm`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

根据上面的提示,将下面的内容添加到.zprofile文件

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

添加完执行source .zprofile

此时,就安装好了,下面列出常用的命令

Example:
  nvm install 8.0.0                     Install a specific version number
  nvm use 8.0                           Use the latest available 8.0.x release
  nvm run 6.10.3 app.js                 Run app.js using node 6.10.3
  nvm exec 4.8.3 node app.js            Run `node app.js` with the PATH pointing to node 4.8.3
  nvm alias default 8.1.0               Set default node version on a shell
  nvm alias default node                Always default to the latest available node version on a shell

  nvm install node                      Install the latest available version
  nvm use node                          Use the latest version
  nvm install --lts                     Install the latest LTS version
  nvm use --lts                         Use the latest LTS version

  nvm set-colors cgYmW                  Set text colors to cyan, green, bold yellow, magenta, and white

国外的node有些慢,可以替换成国内的地址,修改国内源,执行:

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

这样设置之后,nodejs可以从国内镜像地址下载,但是npm不是,需要设置一下才可以

参考nodejs的设置,很多人可能以为npm是这样设置的:export NVM_NPM_REGISTRY=https://registry.npm.taobao.org,亲测无效

正确的设置方法是直接执行:

npm config set registry https://registry.npmmirror.com

这样就好了,全局都是这个地址,不需要每个版本的nodejs都单独设置

查看npm地址命令如下:

npm config get registry

通过这个命令可以验证

Mac安装nvm

温馨提示:

本文最后更新于2023年03月02日,已超过586天没有更新,若内容或图片失效,请留言反馈。

说明

刚刚买了Mac mini,配置了一下环境,正好需要安装node,记录一下

步骤

本文使用brew安装,确保已经安装了brew包管理工具,若未安装,请参考:

这里先记录一下nvm官网,安装执行:

brew install nvm

安装成功会显示如下内容:

==> Downloading https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api/formul
######################################################################## 100.0%
==> Downloading https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api/cask.j
######################################################################## 100.0%
==> Fetching nvm
==> Downloading https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/nvm-0.39.3
######################################################################## 100.0%
==> Pouring nvm-0.39.3.all.bottle.tar.gz
==> Caveats
Please note that upstream has asked us to make explicit managing
nvm via Homebrew is unsupported by them and you should check any
problems against the standard nvm install method prior to reporting.

You should create NVM's working directory if it doesn't exist:
  mkdir ~/.nvm

Add the following to your shell profile e.g. ~/.profile or ~/.zshrc:
  export NVM_DIR="$HOME/.nvm"
  [ -s "/opt/homebrew/opt/nvm/nvm.sh" ] && \. "/opt/homebrew/opt/nvm/nvm.sh"  # This loads nvm
  [ -s "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm" ] && \. "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm"  # This loads nvm bash_completion

You can set $NVM_DIR to any location, but leaving it unchanged from
/opt/homebrew/Cellar/nvm/0.39.3 will destroy any nvm-installed Node installations
upon upgrade/reinstall.

Type `nvm help` for further information.
==> Summary
🍺  /opt/homebrew/Cellar/nvm/0.39.3: 9 files, 190.5KB
==> Running `brew cleanup nvm`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

根据上面的提示,将下面的内容添加到.zprofile文件

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

添加完执行source .zprofile

此时,就安装好了,下面列出常用的命令

Example:
  nvm install 8.0.0                     Install a specific version number
  nvm use 8.0                           Use the latest available 8.0.x release
  nvm run 6.10.3 app.js                 Run app.js using node 6.10.3
  nvm exec 4.8.3 node app.js            Run `node app.js` with the PATH pointing to node 4.8.3
  nvm alias default 8.1.0               Set default node version on a shell
  nvm alias default node                Always default to the latest available node version on a shell

  nvm install node                      Install the latest available version
  nvm use node                          Use the latest version
  nvm install --lts                     Install the latest LTS version
  nvm use --lts                         Use the latest LTS version

  nvm set-colors cgYmW                  Set text colors to cyan, green, bold yellow, magenta, and white

国外的node有些慢,可以替换成国内的地址,修改国内源,执行:

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

这样设置之后,nodejs可以从国内镜像地址下载,但是npm不是,需要设置一下才可以

参考nodejs的设置,很多人可能以为npm是这样设置的:export NVM_NPM_REGISTRY=https://registry.npm.taobao.org,亲测无效

正确的设置方法是直接执行:

npm config set registry https://registry.npmmirror.com

这样就好了,全局都是这个地址,不需要每个版本的nodejs都单独设置

查看npm地址命令如下:

npm config get registry

通过这个命令可以验证


那年今日
03月
02日
下一篇
MacOS更新Git
赞 (1)

猜您想看

  • Git学习手册

    Git是目前最流行的版本控制系统,很多项目都在使用,记录学习历程。

    2019年12月12日
  • VScode编写C语言中文乱码问题

    在VScode上写C语言,总是出现中文乱码问题,找了好多方法都不管用,最后发现了一个方法解决了问题,特此记录。

    2020年03月04日
  • Logitech G HUB无法安装解决方案

    之前驱动安装上了,但是后来重做了一次系统,然后驱动就一直处于初始化状态,或者就显示磁盘已满。后来找到了解决方法,特此记录。

    2021年02月25日
  • 中国建设银行甘肃省分行2021秋季校招面试经验

    如题,参加了建行的秋招,记录一下,分享经验

    2020年11月29日
  • 简易通讯录C#窗体程序

    可视化编程随手demo

    2020年03月27日
  • Git使用教程

    多人协作开发项目的时候,都是使用GitHub或者GitLab等,新手踩了很多坑,所以做此记录,方便查阅。 Git是目前世界上最先进的分布式版本控制系统(没有之一),更多内容就请自行百度。

    2019年07月24日

评论区(暂无评论)

这里空空如也,快来评论吧~

我要评论

Vaptcha 初始化中...