Mac上 homebrew+node+npm+yarn 安装和踩坑过程(stack overflow上出现的高分坑基本都踩了一遍)

一、第一步:安装homebrew

注:安装homebre的目的主要是为了安装node

终端执行

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

参考官网:https://brew.sh/

错误1

=> xcrun: error: active developer path ("/Applications/Xcode.app/Contents/Developer") does not exist

解决办法:

sudo xcode-select --reset

其实出现这个原因是我之前用镜像安装,未按官网推荐安装时出现了如下错误2

Error: Your Xcode (9.0) is outdated. Please update to Xcode 9.0 (or delete it). Xcode can be updated from the App Store.

按照网上方法无法彻底删除系统自带的Xcode (9.0),所以就去下载安装了个10.0版本,同时把brew环境指向现在的Xcode,执行:

sudo xcode-select -switch /Applications/Xcode.app/

由于这条命令导致了上述第一条错误。(吐槽下brew要基于Xcode的设定太恶心了。)其实出现错误2时stack overflow给出这个办法可以试一下:先下载Xcode新版本,然后安装到Documents文件夹,再执行:

sudo xcode-select -switch ~/Documents/Xcode.app/

如果遇到错误3

A newer Command Line Tools release is available.Update them from Software Update in the App Store.

解决办法:
1、登陆:https://developer.apple.com/download/more/ (要用Apple ID登陆)
2、下载对应版本Xcode的对应Command Line版本,如图:
Mac上 homebrew+node+npm+yarn 安装和踩坑过程(stack overflow上出现的高分坑基本都踩了一遍)
注:错误2和错误3是我用镜像安装时出现的错误

报错误4(错误1发生过后):

fatal: http://android.oa.com/homebrew-core/info/refs not valid: is this a git repository?
Error: Fetching /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core failed!
==> Homebrew is run entirely by unpaid volunteers. Please consider donating:
  https://github.com/Homebrew/brew#donations
Failed during: /usr/local/bin/brew update --force

还是镜像安装时候的遗留问题,brew update无效,执行“brew update”可看到解决办法

解决办法:
执行:

git -C "/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core" remote set-url origin https://github.com/Homebrew/homebrew-core.git

到此Homebrew安装成功,接下来要安装node。

一、第二步:安装node

终端执行:

brew install node

报错:

Error: Calling fails_with :gcc_4_0 is disabled! There is no replacement.
Please report this to the homebrew/core tap:
  /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula/node.rb:34

XUEWENLIAO-MB0:~ xuewenliao$ brew install node
Error: Calling fails_with :gcc_4_0 is disabled! There is no replacement.
Please report this to the homebrew/core tap:
  /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula/node.rb:34

这个问题执行brew doctor查明原因就可以了。这都是由于Homebrew没安装好的问题,具体是什么原因,参考第一步,能出现的错误基本就是那几个。

三、第三步:安装npm

终端执行:

brew install npm

四、第四步:安装yarn

有三种安装方式:通过npm安装,通过Homebrew安装,通过官网的初始化脚本方式安装。前两个没试过,很简单应该不会出什么问题,但从第三个方法安装碰到了一点问题。
前两种方式参考:https://neveryu.github.io/2018/07/20/yarn/

按步骤执行

1、终端执行:curl -o- -L https://yarnpkg.com/install.sh | bash

报错:

> WARNING: GPG is not installed, integrity can not be verified!
> Extracting to ~/.yarn...
> Adding to $PATH...
> We've added the following to your /Users/xuewenliao/.bash_profile
> If this isn't the profile of your current shell then please add the following to your correct profile:
export PATH="$HOME/.yarn/bin:$HOME/.config/yarn/global/node_modules/.bin:$PATH"

错误信息指出需要配置下环境变量,且最后一句显示了yarn的当前路径。

2、配置环境变量:
(1)终端执行:vi /etc/profile
(2) 编辑/etc/profile文件中,加上(每个人的yarn路径可能不同,具体看上一步中的错误信息提示):

export PATH="$HOME/.yarn/bin:$HOME/.config/yarn/global/node_modules/.bin:$PATH"

3、查看是否安装成功:
执行:yarn --version
如果显示yarn版本信息不报错说明安装成功了。

若报错:

Yarn requires Node.js 4.0 or higher to be installed.

说明要安装node,那要怎么安装呢?恭喜你绕了回来,从这篇文章的开头看吧。

总结:

最好按照homebrew、node、npm、yarn的顺序进行安装,这应该是最快最稳最方便的方法。核心在于安装Homebrew,这个装好了剩下的基本都不会出现问题。Homebrew在MacOs的地位相当于Linux下的yum或apt-get,装好了后面干啥都方便。能用官网方法尽量用。