M1 mbp 中的新 RN 项目构建失败。 "Could not find node. Make sure it is in bash PATH or set the NODE_BINARY environment variable."

New RN project in M1 mbp built failed. "Could not find node. Make sure it is in bash PATH or set the NODE_BINARY environment variable."

System:
    OS: macOS 11.3
    CPU: (8) arm64 Apple M1
    Memory: 124.86 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.0.0 - /opt/homebrew/bin/node
    Yarn: Not Found
    npm: 7.10.0 - /opt/homebrew/bin/npm
    Watchman: Not Found
  Managers:
    CocoaPods: 1.10.1 - /Users/chia/.rbenv/shims/pod
  SDKs:
    iOS SDK:
      Platforms: iOS 14.5, DriverKit 20.4, macOS 11.3, tvOS 14.5, watchOS 7.4
    Android SDK: Not Found
  IDEs:
    Android Studio: Not Found
    Xcode: 12.5/12E262 - /usr/bin/xcodebuild
  Languages:
    Java: Not Found
  npmPackages:
    @react-native-community/cli: Not Found
    react: 17.0.1 => 17.0.1 
    react-native: 0.64.0 => 0.64.0 
    react-native-macos: Not Found
  npmGlobalPackages:
    *react-native*: Not Found

新 RN 项目创建成功,但构建失败 Xcode。

错误:

Error: Could not find node. Make sure it is in bash PATH or set the NODE_BINARY environment variable.

Command PhaseScriptExecution failed with a nonzero exit code

node是homebrew安装的,Intel mbp没有这个错误

如何解决这个错误?

路径中的节点

npm -g bin /opt/homebrew/bin

echo $PATH /Users/chia/.pyenv/shims:/Users/chia/.rbenv/shims:/Users/chia/.rbenv/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin

sudo ln -s $(which node) /usr/local/bin/node

解决了。

@pekanchuan 在 的回答对我有用。

sudo ln -s $(which node) /usr/local/bin/node

我想补充一点,如果此命令显示 /usr/local/bin/node: File exists,您需要知道 link 已经存在,可能是不同版本的节点。 在我的例子中,为了安装 yarnbrew 安装了一个单独的 nodejs v15 并将文件 link 编辑为二进制文件。 虽然我用nvmnodejs v14nodejs v16。这个额外的 nodejs 是问题中提到的错误的原因。

只需 运行 sudo rm -f /usr/local/bin/node 删除 link,然后使用命令 sudo ln -s $(which node) /usr/local/bin/node 创建正确的 link.

在我的 Mac M1 上,文件夹 /usr/local/bin 丢失了,所以我遇到了错误:

ln: /usr/local/bin/node: No such file or directory

这就是我所做的:

cd /usr/local
sudo mkdir bin
sudo ln -s $(which node) /usr/local/bin/node