简体   繁体   English

React Native ios 构建:找不到节点

[英]React Native ios build : Can't find node

I have a prototype ready to go and the project is jammed with build:我有一个原型准备好 go 并且项目被构建卡住了:

error: Can't find 'node' binary to build React Native bundle If you have non-standard nodejs installation, select your project in Xcode, find 'Build Phases' - 'Bundle React Native code and images' and change NODE_BINARY to absolute path to your node executable (you can find it by invoking 'which node' in the terminal)错误:找不到'node'二进制文件来构建React Native包如果你有非标准的nodejs安装,select你的项目在Xcode中,找到'Build Phases' - 'Bundle React Native code and images'并将NODE_BINARY更改为绝对路径到您的节点可执行文件(您可以通过在终端中调用“哪个节点”来找到它)

this feedback is helpless for me, i do have node with nvm.这个反馈对我来说是无助的,我确实有带有 nvm 的节点。 is this something related to bash?这与 bash 有关吗?

@brunocascio solution on the comment is simpler and less invasive, create a symlink to node, on command line:评论上的@brunocascio 解决方案更简单,侵入性更小,在命令行上创建一个指向节点的符号链接:

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

Update:更新:

On new M1 Mac I had to cd /usr/local then mkdir bin (or just sudo mkdir /usr/local/bin) first.在新的 M1 Mac 上,我必须先 cd /usr/local 然后是 mkdir bin(或只是 sudo mkdir /usr/local/bin)。

thanks leo for the comment感谢 leo 的评论

I found one solution我找到了一个解决方案

First find your current node, in shell首先在shell中找到你当前的节点

which node

then copy your node url to然后将您的节点网址复制到

export NODE_BINARY=[your node path]
../node_modules/react-native/packager/react-native-xcode.sh to node_modules/react-native/scripts/react-native-xcode.sh

在此处输入图片说明

Solution for nvm users : nvm 用户的解决方案:

In your build phases scripts, just add在您的构建阶段脚本中,只需添加

# Fix for machines using nvm
if [[ -s "$HOME/.nvm/nvm.sh" ]]; then
. "$HOME/.nvm/nvm.sh"
elif [[ -x "$(command -v brew)" && -s "$(brew --prefix nvm)/nvm.sh" ]]; then
. "$(brew --prefix nvm)/nvm.sh"
fi

Above export NODE_BINARY=node .以上export NODE_BINARY=node This will make Xcode work regardless of your machine using nvm.无论您使用 nvm 的机器如何,这都将使 Xcode 工作。

The solution for me is to set a default version of node with nvm in your profile.我的解决方案是在您的配置文件中使用nvm设置node的默认版本。 This works for bash or zsh:这适用于 bash 或 zsh:

Add this to your .zshrc or .bashrc将此添加到您的.zshrc.bashrc

# default node version for nvm
nvm use 8.9.3

Be sure to change it to the version you want when starting a new terminal.确保在启动新终端时将其更改为您想要的版本。

If you are developing a React Native based solution and you are using NVM for local Node versioning, the error may be due to this.如果您正在开发基于 React Native 的解决方案,并且您正在使用 NVM 进行本地 Node 版本控制,则错误可能是由于此。

XCode cannot find the Node version , of course XCode fetches the Node in the / usr / local / bin / node directory and NVM stores the Node in another directory like Users / $ {my-user} /. Nvm / versions /node/v14.16.0/bin/node XCode 找不到 Node 版本,当然 XCode 会fetches the Node in the / usr / local / bin / node directory而 NVM 将 Node 存储在另一个目录中,如Users / $ {my-user} /. Nvm / versions /node/v14.16.0/bin/node Users / $ {my-user} /. Nvm / versions /node/v14.16.0/bin/node

To work it is enough to create an alias for XCode to find the Node in its default search:为了工作,为 XCode 创建一个别名就足以在其默认搜索中找到节点:

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

nvm alias default 14

      or

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

这使我的 xcode 12.4 看到节点

If you are using Mac, check if you have installed node with brew.如果您使用的是 Mac,请检查您是否使用 brew 安装了节点。 In this case it cannot find the path.在这种情况下,它找不到路径。 Install node by downloading from the official website从官网下载安装node

If this command says /usr/local/bin/node: File exists you need to know that the link already exists to maybe a different version of node.如果此命令显示 /usr/local/bin/node: File exists 您需要知道该链接可能已经存在到不同版本的节点。 In my case, to install yarn, brew installed a separate nodejs v15 and linked the file to its binary.就我而言,要安装纱线,brew 安装了一个单独的 nodejs v15 并将文件链接到其二进制文件。 Although I use nvm to have nodejs v14 and nodejs v16.虽然我使用 nvm 来拥有 nodejs v14 和 nodejs v16。 This extra nodejs was the reason for the error mentioned in question.这个额外的 nodejs 是所提到的错误的原因。

Simply run sudo rm -f /usr/local/bin/node to remove the link followed by the command sudo ln -s $(which node) /usr/local/bin/node to create correct link.只需运行 sudo rm -f /usr/local/bin/node 删除链接,然后执行命令 sudo ln -s $(which node) /usr/local/bin/node 创建正确的链接。

The best solution is to actually do ln -s $(which node) /usr/local/bin , this will create a "symlink" from /usr/local/bin/node to wherever your node is, this is important because most apps looks at node at this path.最好的解决方案是实际执行ln -s $(which node) /usr/local/bin ,这将创建从/usr/local/bin/node到您节点所在位置的“符号链接”,这很重要,因为大多数应用程序查看此路径上的节点。 You probably don't want to do export NODE_BINARY=[your node path] because when another teammate has a different OS or different installation of node (ie, one is using nvm the other is using homebrew), then the same error will occur, it will also happen when building for production.你可能不想做export NODE_BINARY=[your node path]因为当另一个队友有不同的操作系统或不同的节点安装时(即,一个使用 nvm 另一个使用自制软件),那么同样的错误会发生,在为生产而构建时也会发生这种情况。 So just go with ln -s $(which node) /usr/local/bin to be safe.所以只需使用ln -s $(which node) /usr/local/bin就安全了。

For anyone that stumbles upon this in 2022 here is my situation and how I fixed it.对于在 2022 年偶然发现此问题的任何人,这就是我的情况以及我如何解决它。

  1. react-native --version -> 4.14 react-native --version -- react-native --version -> 4.14
  2. npx --version -> 6.14.9 npx -- npx --version -> 6.14.9
  3. node --version -> 12.14.1 node --version -- node --version -> 12.14.1
  4. We use TypeScript also.我们也使用 TypeScript。

XCode "Bundle React Native code and images" XCode“捆绑 React Native 代码和图像”

export ENTRY_FILE=src/App.tsx
../node_modules/react-native/scripts/react-native-xcode.sh
../node_modules/expo-constants/scripts/get-app-config-ios.sh
../node_modules/expo-updates/scripts/create-manifest-ios.sh

Remove the export NODE_BINARY=node line of code.删除export NODE_BINARY=node代码行。 It's not needed anymore.它不再需要了。 The way I figured this out was reading through the source code found in ../node_modules/react-native/scripts/react-native-xcode.sh If you look there, you'll find nvm/node sourcing being done for you.我发现这一点的方法是阅读../node_modules/react-native/scripts/react-native-xcode.sh的源代码。

From the React Native environment setup documentation :来自 React Native 环境设置文档

Starting from React Native version 0.69, it is possible to configure the Xcode environment using the.xcode.env file provided by the template.从 React Native 0.69 版本开始,可以使用模板提供的 .xcode.env 文件来配置 Xcode 环境。

The.xcode.env file contains an environment variable to export the path to the node executable in the NODE_BINARY variable. .xcode.env 文件包含一个环境变量,用于将路径导出到 NODE_BINARY 变量中的节点可执行文件。 This is the suggested approach to decouple the build infrastructure from the system version of node.这是将构建基础架构与节点的系统版本分离的建议方法。 You should customize this variable with your own path or your own node version manager, if it differs from the default.如果它与默认值不同,您应该使用您自己的路径或您自己的节点版本管理器自定义此变量。

so in your project's root directory, look in the ios/ subdirectory, create or update the file .xcode.env to export your node binary:因此,在您项目的根目录中,查看ios/子目录,创建或更新文件.xcode.env以导出您的节点二进制文件:

file: /ios/.xcode.env文件: /ios/.xcode.env

# To use nvm with brew, uncomment the line below
# . "$(brew --prefix nvm)/nvm.sh" --no-use
export NODE_BINARY=$(command -v node)

This file will be loaded by Xcode and properly provide the node binary for your build process.该文件将由 Xcode 加载,并为您的构建过程正确提供节点二进制文件。 Note that if you're using NVM for your node environment, you'll need to uncomment the line to initialize your NVM environment before exporting the node binary.请注意,如果您在节点环境中使用 NVM,则需要取消注释该行以在导出节点二进制文件之前初始化 NVM 环境。

Hope this helps: :)希望这可以帮助: :)

Open Xcode, then choose "Preferences..." from the Xcode menu.打开 Xcode,然后从 Xcode 菜单中选择“首选项...”。

Go to the Locations panel and install the tools by selecting the most recent version in the Command Line Tools dropdown.转到位置面板并通过在命令行工具下拉列表中选择最新版本来安装工具。

select command line tools like as this image选择像这张图片这样的命令行工具在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM