Mac 通过安装node、nvm、nrm、yarn;macOS 13

3 篇文章 0 订阅

m1版本

先安装homebrew,参照:https://blog.csdn.net/CaptainJava/article/details/109135437https://blog.csdn.net/CaptainJava/article/details/109135437

安装nvm 

# 执行

brew install nvm

# 输出如下:

homebrew-core git:(master) brew install nvm
==> Downloading https://mirrors.aliyun.com/homebrew/homebrew-bottles/nvm-0.39.1_1.all.bottle.tar.gz
######################################################################## 100.0%
==> Pouring nvm-0.39.1_1.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 ~/.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.
==> Summary
🍺  /opt/homebrew/Cellar/nvm/0.39.1_1: 9 files, 184KB
==> Running `brew cleanup nvm`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

按照输出命令

创建.nvm目录

mkdir ~/.nvm

修改~/.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

配置淘宝源

# 配置nvm国内淘宝源
export NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node/

source ~/.zshrc 

安装node 

# 安装node最新稳定版本,目前黑丝18.1.0
# 执行
nvm install v18.1.0

# 输出如下:
Downloading and installing node v18.1.0...
Downloading https://nodejs.org/dist/v18.1.0/node-v18.1.0-darwin-arm64.tar.xz...
################################################################################################## 100.0%
Computing checksum with shasum -a 256
Checksums matched!
Now using node v18.1.0 (npm v8.8.0)
Creating default alias: default -> v18.1.0

# 查看已安装版本
# 执行
nvm ls

# 输出如下:             
->      v18.1.0
default -> v18.1.0
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v18.1.0) (default)
stable -> 18.1 (-> v18.1.0) (default)
lts/* -> lts/gallium (-> N/A)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.12 (-> N/A)
lts/fermium -> v14.19.3 (-> N/A)
lts/gallium -> v16.15.1 (-> N/A)

# 查看node版本
node -v

# 输出:
v18.1.0

 nvm相关命令

nvm install stable ## 安装最新稳定版 node,当前是node v9.5.0 (npm v5.6.0)

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 包

nvm alias default <version> ## 设置默认nodejs版本

 安装nrm

npm install nrm -g

nrm命令

nrm ls        	--当前可用源
nrm use 源名称    	--切换源 如nrm use npm 切换回npm下载源 切回淘宝源nrm use taobao 即可
nrm test        	--检测所有源的速度(至今未使用过)

以后用nrm use +"源名称"  来切换,这样就很方便了 不用我们每次记住详细地址。

测速 

nrm test

* npm ------ 1355ms
  yarn ----- 2285ms
  tencent -- 1321ms
  cnpm ----- 625ms
  taobao --- 180ms
  npmMirror - 2652ms

可以看出淘宝源最快,

切换淘宝源

nrm use taobao
                        

   Registry has been set to: https://registry.npmmirror.com/

查看源切换是否成功

npm config get registry

安装yarn

# 安装
npm install --global yarn


# 查看版本
yarn --version

设置淘宝镜像

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

查看当前地址

yarn config get registry

还原地址

yarn config set registry https://registry.yarnpkg.com

YARN镜像的管理工具

安装:yarn global add yrm

# 查看所有镜像
yrm ls

  npm ---------- https://registry.npmjs.org/
* yarn --------- https://registry.yarnpkg.com/
  tencent ------ https://mirrors.cloud.tencent.com/npm/
  cnpm --------- https://r.cnpmjs.org/
  taobao ------- https://registry.nlark.com/
  npmMirror ---- https://skimdb.npmjs.com/registry/

# 使用淘宝镜像
yrm use taobao

***一定注意源地址不能带引号

electron镜像设置

yarn config set electron_mirror https://npmmirror.com/mirrors/electron/


 


历史版本


nvm:nodejs 版本管理工具。
即,nvm 可以安装node 并可以切换本地使用版本。
nodejs:在项目开发时的所需要的代码库
npm:nodejs 包管理工具。在安装的 nodejs 的时候,npm 也会跟着一起安装,它是包管理工具。 管理 nodejs 中的第三方插件。
nrm:是node下载源的管理工具。由于node默认下载源很慢我们一般会使用淘宝的源,使用nrm可以很方便的切换下载源。

nvm、nodejs、npm、nrm的关系:nvm 管理 nodejs 和 npm 的版本。npm 可以管理 nodejs 的第三方插件。nrm来管理node或者说npm的下载源地址。

一、安装nvm(推荐,省事儿)

1、①brew安装的nvm总是出问题,所以用一下方式安装,其中v0.33.11为本次安装的版本号,也可以替换为自己喜欢的版本。

最新安装命令和版本见:

GitHub - nvm-sh/nvm: Node Version Manager - POSIX-compliant bash script to manage multiple active node.js versionsNode Version Manager - POSIX-compliant bash script to manage multiple active node.js versions - GitHub - nvm-sh/nvm: Node Version Manager - POSIX-compliant bash script to manage multiple active node.js versionshttps://github.com/nvm-sh/nvm2021-11-03 最新版本为v0.39.0

安装命令为:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
# 或者
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash

②或者用git命令下载最新稿版本安装脚本(推荐)

git clone git://github.com/creationix/nvm.git ~/nvm
 cd ~/nvm
 ./install.sh 

③或者手动下载最新版本安装文件(推荐)

浏览器打开 nvm/install.sh at master · nvm-sh/nvm · GitHub 把install.sh文件保存到服务器~/nvm下

  依次执行一下命令

cd ~/nvm
chmod +x install.sh
./install.sh

以上三种方式安装成功后都会显示如下输出

④github提供的安装方式

 GitHub - nvm-sh/nvm: Node Version Manager - POSIX-compliant bash script to manage multiple active node.js versions

2、配置nvm命令

安装完后会提示重启终端或者运行以下三行命令,由于我的mac用的是zsh 所以我在~/.zshrc里配置(本次安装的默认将这三条命令添加到了~/.bash_profile中,如果你用的是bash 那就不用额外配置了。linux系统是在~/.bashrc下)

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

顺便配置一下nvm镜像源为淘宝源

export NVM_NODEJS_ORG_MIRROR=http://npm.taobao.org/mirrors/node
export NVM_IOJS_ORG_MIRROR=http://npm.taobao.org/mirrors/iojs

让配置生效

source ~/.zshrc

linux下执行:source ~/.bashrc

3.window10安装nvm

    下载nvm安装包nvm-setup.zip,地址:Releases · coreybutler/nvm-windows · GitHub

     解压后双击安装即可,但是要注意,默认路径自己设置一下自己想要安装的路径、但是一定不能有空格汉字等字符,全英文路径,比如:D:/nvm  、   D:/nodejs,如果有空格或者汉字在使用nvm use命令时会报错。后边内容window、linux、mac是一样的。

查看一下版本

nvm --version

nvm相关命令

nvm install stable ## 安装最新稳定版 node,当前是node v9.5.0 (npm v5.6.0)

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 包

nvm alias default <version> ## 设置默认nodejs版本

二、安装node

查看最新版本,地址:Node.js,目前最新的长期支持版本(LTS)是14.15.4  可以使用nvm install 14.15.4

我这里安装的使6.11.1 你可以替换为你想用的版本。并不是越高也好,要看你是干什么用。没有要求的话一般是安装长期支持版本的最新版。我这里是制定版本了。

nvm install v6.11.1

ok node安装好了 可以检查一下版本

 ~  node --version
v6.11.1
 ~  npm --version
3.10.10

三、安装nrm

1、首先切换淘宝镜像源

npm config set registry https://registry.npm.taobao.org

2、可以查看是否切换成功 

npm config get registry

 现在用npm安装插件就会快很多很多。试过的人都知道。

当然了, 为什么还是要安装nrm囔,因为我这是给区块链配的环境,有时候淘宝源并不是所有npm包都是好用的,偶尔会切换其他源来安装依赖环境,所以干脆安装nrm,会更方便。

3、安装nrm

npm install nrm -g

nrm命令

nrm ls        	--当前可用源
nrm use 源名称    	--切换源 如nrm use npm 切换回npm下载源 切回淘宝源nrm use taobao 即可
nrm test        	--检测所有源的速度(至今未使用过)

以后用nrm use +"源名称"  来切换,这样就很方便了 不用我们每次记住详细地址。

ok,node环境完美了。

升级macos13后使用brew install nvm 报以下错:

➜  nvm2 brew install nvm
fatal: not in a git directory
Warning: No remote 'origin' in /opt/homebrew/Library/Taps/homebrew/homebrew-cask, skipping update!
fatal: not in a git directory
Warning: No remote 'origin' in /opt/homebrew/Library/Taps/homebrew/homebrew-core, skipping update!
fatal: not in a git directory
Warning: No remote 'origin' in /opt/homebrew/Library/Taps/homebrew/homebrew-services, skipping update!
==> Downloading https://mirrors.aliyun.com/homebrew/homebrew-bottles/nvm-0.39.2.
Already downloaded: /Users/tankerma/Library/Caches/Homebrew/downloads/92ce6c990ede7bd0f5bd5587c819ec54622ba3c6b940e930375e5b0737f608d0--nvm-0.39.2.all.bottle.tar.gz
fatal: not in a git directory
Error: Command failed with exit 128: git

解决方案:

增加git安全目录:

git config --global --add safe.directory /opt/homebrew/Library/Taps/homebrew/homebrew-cask

git config --global --add safe.directory /opt/homebrew/Library/Taps/homebrew/homebrew-core

git config --global --add safe.directory /opt/homebrew/Library/Taps/homebrew/homebrew-services

如果git没有设置邮箱和用户名也会提示:fatal: not in a git directory

修改:

git config --global user.name "yourName" //注意,--和global之间没有空格
git config --global user.email "yourName@gmail.com"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值