【教程】Homebrew安装NVM & Nodejs

✨Hombrew

推荐阅读:

【macOS】Homebrew & Homebrew cask macOS软件包管理神器 - 双份浓缩馥芮白 - 博客园 (cnblogs.com)


✨NVM

nvm is a version manager for node.js, designed to be installed per-user, and invoked per-shell. nvm works on any POSIX-compliant shell (sh, dash, ksh, zsh, bash), in particular on these platforms: unix, macOS, and windows WSL.


https://github.com/nvm-sh/nvm


✨brew安装NVM

安装

brew install nvm

阅读注意事项

==> 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 ~/.zshrc or your desired shell
configuration file:

  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/opt/nvm will destroy any nvm-installed Node installations
upon upgrade/reinstall.

Type `nvm help` for further information.

相关配置

创建目录

mkdir ~/.nvm

配置环境变量

echo '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' >> ~/.zshrc

配置生效

source ~/.zshrc

卸载

Note:
to remove, delete, or uninstall nvm - just remove the $NVM_DIR folder (usually ~/.nvm)

rm -rf ~/.nvm

✨NVM安装Nodejs

查看帮助

查看nvm相关帮助

nvm --help

或者访问nvm Github主页 查看帮助


NVM使用

要下载、编译和安装最新版本的 node,请执行以下操作:

nvm install node # "node" is an alias for the latest version

要安装特定版本的节点:

nvm install 14.7.0 # or 16.3.0, 12.22.1, etc

安装的第一个版本成为默认版本。新的 shell 将以节点的默认版本开始(例如,nvm alias default)。

您可以使用以下命令列出可用版本ls-remote

nvm ls-remote

然后在任何新的 shell 中使用已安装的版本:

nvm use node

或者你可以运行它:

nvm run node --version

或者,您可以使用所需版本的节点在子 shell 中运行任意命令:

nvm exec 4.2 node --version

您还可以获取可执行文件的安装路径:

nvm which 12.22

代替“14.7”或“16.3”或“12.22.1”之类的版本指针,您可以将以下特殊默认别名与nvm install, nvm use, nvm run, nvm exec,nvm which等一起使用:

  • node:这将安装最新版本的node
  • iojs:这将安装最新版本的io.js
  • stable:此别名已弃用,仅适用于node v0.12及更早版本。目前,这是node.
  • unstable:此别名指向node v0.11- 最后一个“不稳定”节点版本,自 1.0 之后,所有节点版本都是稳定的。(在 SemVer 中,版本传达的是破损,而不是稳定性)。

详细可访问nvm Github主页


安装Nodejs长期支持版本

nvm install --lts


✨npm配置

查看当前版本

node -v
npm -v

更换国内源

!注意npm淘宝镜像源域名已更新

点击查看

!因此更换源时请务必选择新的域名

npm config set registry=http://registry.npmmirror.com

查看配置信息

npm config list


⭐转载请注明出处

本文作者:双份浓缩馥芮白

原文链接:https://www.cnblogs.com/Flat-White/p/16421281.html

版权所有,如需转载请注明出处。

posted @ 2022-06-28 21:53  双份浓缩馥芮白  阅读(2049)  评论(2编辑  收藏  举报