92

I have a prototype ready to go and the project is jammed with build:

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)

this feedback is helpless for me, i do have node with nvm. is this something related to bash?

Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
Seeliang
  • 2,321
  • 1
  • 13
  • 18
  • 1
    your node version ? – Arunkumar Jun 12 '17 at 05:53
  • 3
    Are you using nvm? – Steve Potter Jun 12 '18 at 21:33
  • This happened to me as well with an Expo Bare Workflow project: Node is installed via `nodenv` and Expo is installed via `expo init ExpoBareTest`. During the build, the `node` binary cannot be found, even though Xcode builds are working with regular (non-Expo) React Native projects.- Both solutions given below, the `export NODE_BINARY=/path/to/node` as well as `ln -s $(which node) /usr/local/bin/node`, work correctly in this case. (Just adding this as neither Expo nor nodenv was mentioned in this context before.) – ChrisW May 25 '21 at 08:48

21 Answers21

288

@brunocascio solution on the comment is simpler and less invasive, create a symlink to node, on command line:

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.

thanks leo for the comment

josesuero
  • 3,260
  • 2
  • 13
  • 19
  • 3
    I think this is a much cleaner solution. The modification to build phase solution would cause a modification to the project which may impact other team members working on the same project with different env setup. – jyliang Mar 11 '19 at 17:03
  • 6
    for `nvm` users, this is the solution to building your `react-native` app on Xcode when facing `env: node: No such file or directory`. – kenmistry May 28 '19 at 05:14
  • Thank you so much. I just set up a new Mac and couldn't figure out why I was getting this error when building my app, despite my setup being the same as my old Mac (Node.js installed via n, for example). Regardless, this fixed it so thanks again. – Darryl Young Nov 13 '20 at 10:45
  • 4
    On new M1 Mac I had to `cd /usr/local` then `mkdir bin` (or just `sudo mkdir /usr/local/bin`) first. – Leo Jul 11 '21 at 10:35
  • 2
    This messed up anything to do with node on my machine for me... I couldn't run other node commands like `gatsby develop` or `ntl dev` for example... I fixed the issue by reverting and deleting `node` in `/usr/local/bin` – fullStackChris Oct 15 '21 at 13:13
  • Brilliant ! Thank you for @josesuero !! – rudenick Jan 17 '22 at 03:51
  • You might want to create a symlink to actual node dir if you're using nodeenv: `ln -s $(nodenv which node) /usr/local/bin/node` – Arthur Wang Jan 17 '22 at 09:28
  • I use a M1 Mac and i get the followed issue: `ln: /usr/local/bin/node: Permission denied` – gigeos Jun 20 '22 at 12:40
  • Ok fixed with sudo en restarting my Mac – gigeos Jun 20 '22 at 13:05
  • put sudo in front of answer if you get Permission Denied —> `sudo ln -s $(which node) /usr/local/bin/node` – Zernach Sep 26 '22 at 18:33
  • it is still working. Thanks! – Hakan Saglam Jun 07 '23 at 14:19
83

I found one solution

First find your current node, in 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

enter image description here

Philipp Kyeck
  • 18,402
  • 15
  • 86
  • 123
Seeliang
  • 2,321
  • 1
  • 13
  • 18
  • 61
    It could be better creating a symlink as following: `ln -s $(which node) /usr/local/bin/node` and leave the ios app configuration as default. :) – brunocascio Oct 10 '17 at 23:00
  • 1
    great! you saved me :D – bkit4u Feb 08 '18 at 10:02
  • 4
    Hi, path has changed from `node_modules/react-native/packager/react-native-xcode.sh` to `node_modules/react-native/scripts/react-native-xcode.sh` – dreadcast May 04 '18 at 08:34
  • For me below worked as mentioned by dreadcase. export NODE_BINARY=/usr/local/bin/node node_modules/react-native/scripts/react-native-xcode.sh to node_modules/react-native/scripts/react-native-xcode.sh – Muhammad Maqsood Apr 30 '21 at 17:29
  • 6
    Do not do this, this is a hack and will make your build brittle. This is committing a hard-coded environment configuration to your repo. – Philihp Busby Mar 30 '22 at 18:08
  • I always come to this answer ..... – Tushar Pandey Aug 29 '22 at 12:28
38

Solution for nvm users :

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. This will make Xcode work regardless of your machine using nvm.

Maxime B
  • 511
  • 4
  • 7
15

For anyone that stumbles upon this in 2022 here is my situation and how I fixed it.

  1. react-native --version -> 4.14
  2. npx --version -> 6.14.9
  3. node --version -> 12.14.1
  4. We use TypeScript also.

XCode "Bundle React Native code and images"

enter image description here

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. 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.

johndpope
  • 5,035
  • 2
  • 41
  • 43
Alex
  • 952
  • 7
  • 12
12
nvm alias default 14

OR

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

This made my Xcode 12.4 see node

Rashid Latif
  • 2,809
  • 22
  • 26
F Mamali
  • 121
  • 1
  • 4
9

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.

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

To work it is enough to create an alias for XCode to find the Node in its default search:

sudo ln -s $(which node) /usr/local/bin/node
Simon L. Brazell
  • 1,132
  • 8
  • 14
Roberto
  • 91
  • 1
  • 1
  • 1
    Although this solution is mentioned in various answers, ++ to @Roberto for providing an explanation. – SJaka Sep 19 '22 at 12:43
6

In 2022 the default build looks in ios/.xcode.env.local and ios/.xcode.env for environment customizations. The provided .xcode.env has an example enabling nvm and setting NODE_BINARY.

This is configured in Build Phases -> Bundle React Native code and images. The shell script looks as follows circa 10/2022:

set -e

WITH_ENVIRONMENT="../node_modules/react-native/scripts/xcode/with-environment.sh"
REACT_NATIVE_XCODE="../node_modules/react-native/scripts/react-native-xcode.sh"

/bin/sh -c "$WITH_ENVIRONMENT $REACT_NATIVE_XCODE"

The Input Files section for this script has

$(SRCROOT)/.xcode.env.local
$(SRCROOT)/.xcode.env

The default .xcode.env looks like this now:

# This `.xcode.env` file is versioned and is used to source the environment
# used when running script phases inside Xcode.
# To customize your local environment, you can create an `.xcode.env.local`
# file that is not versioned.

# NODE_BINARY variable contains the PATH to the node executable.
#
# Customize the NODE_BINARY variable here.
# For example, to use nvm with brew, add the following line
#. "$(brew --prefix nvm)/nvm.sh" --no-use
export NODE_BINARY=$(command -v node)

To reliably get node set, configure your project properties to the current default values and update one of the two reference env files.

My ios/.xcode.env looks like this:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
export NODE_BINARY=$(command -v node)
Glenn
  • 1,996
  • 2
  • 24
  • 32
  • it compiles and spits out the build - but is it the wrong node version? I'm using nvm 16 - but system wants 17. how to target nvm 16? – johndpope Mar 29 '23 at 06:31
  • See above where I added my example .xcode.env file. You need to load nvm so it'll pick up your selected nvm default. – Glenn Mar 29 '23 at 15:03
5

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. 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 (i.e., one is using nvm the other is using homebrew), then the same error will occur, it will also happen when building for production. So just go with ln -s $(which node) /usr/local/bin to be safe.

aprilmintacpineda
  • 1,114
  • 13
  • 21
4

For users who migrate to react-native 0.70.* and get a similar error, I fixed it by adding in Build Phases section:

set -e

WITH_ENVIRONMENT="../node_modules/react-native/scripts/xcode/with-environment.sh"
REACT_NATIVE_XCODE="../node_modules/react-native/scripts/react-native-xcode.sh"

/bin/sh -c "$WITH_ENVIRONMENT $REACT_NATIVE_XCODE"

enter image description here

And in the iOS directory create the file .xcode.env.local (doc)

# This `.xcode.env` file is versioned and is used to source the environment
# used when running script phases inside Xcode.
# To customize your local environment, you can create an `.xcode.env.local`
# file that is not versioned.
# NODE_BINARY variable contains the PATH to the node executable.
#
# Customize the NODE_BINARY variable here.
# For example, to use nvm with brew, add the following line
# . "$(brew --prefix nvm)/nvm.sh" --no-use
export NODE_BINARY=$(which node)

3

The solution for me is to set a default version of node with nvm in your profile. This works for bash or zsh:

Add this to your .zshrc or .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.

TechnoTim
  • 3,065
  • 1
  • 23
  • 28
3

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. In my case, to install yarn, brew installed a separate nodejs v15 and linked the file to its binary. Although I use nvm to have nodejs v14 and nodejs v16. This extra nodejs was the reason for the error mentioned in question.

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.

Muteshi
  • 820
  • 1
  • 10
  • 25
Preetika
  • 702
  • 8
  • 21
2

From the React Native environment setup documentation:

Starting from React Native version 0.69, it is possible to configure the Xcode environment using the .xcode.env file provided by the template.

The .xcode.env file contains an environment variable to export the path to the node executable in the NODE_BINARY variable. 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:

file: /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. 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.

Hope this helps! :)

John F
  • 865
  • 1
  • 8
  • 15
2

This error occurs if the path of the "node" changes somehow. I have solved this issue by using this command:

sudo ln -s $(which node) /usr/local/bin/node"
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
2

In my case, I had to choose a lower version of node
from node/18.7.0 to node/16.9.0


react-native: 0.66.3
Xcode: 14.1
MacOs: 12.6.1

0

Simple latest working solution:

Just replace this line in shell:

export NODE_BINARY=node ../node_modules/react-native/scripts/react-native-xcode.sh

ScreenShot

Ajmal Hasan
  • 885
  • 11
  • 9
0

If you use the NVM tool to manage different versions of the Node then creating a symbolic link to the current default Node version ln -s $(which node) /usr/local/bin/node could break the other projects those expect another version. That happens as in the PATH environment variable the path /usr/local/bin goes before the path of the Node selected by NVM.

React Native and Expo uses the bash shell for build scripts and therefore we just need to set up the bash shell properly to work with NVM. To do that copy the configuration code from the ~/.zshenv into ~/.bash_profile without taking the autocompletion part:

# This file is for the React Native development as it requires the bash shell in the build scripts

# Configure NVM
export NVM_DIR="$HOME/.nvm"
[ -s "/opt/homebrew/opt/nvm/nvm.sh" ] && . "/opt/homebrew/opt/nvm/nvm.sh"
EddiG
  • 172
  • 2
  • 4
0

I will give you the quickest solution not the best.

Open terminal and write which node or command -v node to find out where is your node. It will give you path for me its: /opt/homebrew/bin/node.

Then go to script_phases.sh file under your PROJECT_ROOT/node_modules/react-native/scripts/react-native-pods-utils/script_phases.sh and paste this top of the file.

NODE_BINARY="/opt/homebrew/bin/node"

You might do the same thing for react-native-xcode.sh under the PROJECT_ROOT/node_modules/react-native/scripts/.

0

If you are using NVM, then the usual cause of this error is NVM not properly initialized in the Bundle React Native Code And Images Build Phase.

To initialize NVM just change the Build Phase script like so:

source $HOME/.zshrc
# If you initialize NVM using .profile uncomment this line:
# source $HOME/.profile
export NODE_BINARY=$(which node)
../node_modules/react-native/scripts/react-native-xcode.sh
Al Momo
  • 33
  • 3
0

In my case I am not fixing the React Native project but I use a React Native integration in my native project. Some dependencies were not seeing the node I had installed using homebrew, although I had it on PATH.

For anyone that stumbles upon the same situation just make sure the /usr/local/bin directory exists and run

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

as mentioned by @F Mamali and @josesuero

-1

If you are using Mac, check if you have installed node with brew. In this case it cannot find the path. Install node by downloading from the official website

user3009752
  • 164
  • 2
  • 9
  • 24
-6

Open Xcode, then choose "Preferences..." from the Xcode menu.

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 enter image description here

Arunkumar
  • 1,705
  • 1
  • 17
  • 25